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;
}

Recommended Answers

All 2 Replies

The variable "a" is declared inside "main()" scope. You cannot simply refer to it in other member function of the class - Choice(). Your function is quite a mess there...

Also why do you use "for" loop in place of "while" loop? Even though it gives the same effect, I still think it is not really a good practice.

for(;(x<'0') || (x>'7');) {
  ...
}

// should be...

char x = '0';  // declare x first
while(x<'0' || x>'7') {
  ...
}

// or

do {
  ...
} while (x<'0' || x>'7');

Hi. You haven't declared object a in the scope where you're calling SavePlayer.
My advice is to make SavePlayer a static function, since you don't need to access member variables. On the other hand I don't see how the InputData() call can work, since you need an object to call it on.
Looks like your code should be more like:
Player a;
a.InputData(); // This initializes a from stdin
SavePlayer(manyPlayer, a);

and declare SavePlayer like:
static void SavePlayer(vector<Player>& manyPlayer, Player &a);

I also don't see where that manyPlayer reference comes from. It will also be out of scope, since it's only declared as a local variable to main(), but you have enough info to fix that :)

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.