Within this code I would like to save the data which was input through a function
"SavePlayer", but it doesn't work. The compiler "says" that object "a" ist not known.
Probably the bold line
[B][I]a.SavePlayer(manyPlayer,Player a); [/I][/B]
is wrong, but I have no idea how to solve the problem. Maybe someone could help?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//create class
class Player
{
public:
string Nachname;
string Vorname;
void Choice();
void SavePlayer(vector<Player>&manyPlayer,Player &a);
void InputData();
};
void Player::InputData()
{
cout<<"Nachname: ";
cin>>this->Nachname;
//cin>>(*this).Nachname;
cout<<"Vorname: ";
cin>>this->Vorname;
}
void Player::Choice()
{
char x;
cin>>x;
for(;(x<'0') || (x>'7');)
{
cout<<"Ungueltige Eingabe, bitte wiederholen."<<endl;
cin.sync(); //removing any unread character from the standard input queue of characters.
cin>>x;
}
switch(x)
{
case '1': cout<<"Your choice has been 1."<<endl;
//calling function for the input of data
InputData();
//should save the data in vector, but it doesn't work.
[B][I]a.SavePlayer(manyPlayer,Player a); [/I][/B]
break;
}
}
void Player::SavePlayer(vector<Player>&manyPlayer,Player &a)
{
manyPlayer.push_back(a);
}
int main()
{
//create vector
vector<Player>manyPlayer;
//create object
Player a;
a.Choice(); //starting function for input of data
//In the main saving the vector works....
//a.SavePlayer(manyPlayer,a);
//initialize iterator
vector<Player>::iterator pos;
//output through iterator
for(pos=manyPlayer.begin();pos!=manyPlayer.end();++pos)
{
cout<<pos->Vorname<<' '<<pos->Nachname<<endl;
}
return 0;
}