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

Dani AI

Generated

The error is a plain scope problem: the object and container you created in main() are not visible inside the member function Choice(). You also cannot declare a variable inside a call (writing a type name inside the call is illegal). That is why the compiler reports the name as unknown.

Practical ways to fix it (pick one depending on design):

  1. Keep ownership in main. Call the input routine on the Player object you create in main and then append that object to the vector there. This is the simplest and clearest approach for small programs.

  2. Let Choice operate on the container by passing it in. Change Choice to accept a vector<Player>& parameter; from main call a.Choice(manyPlayer). Inside Choice, after InputData() push the current object into the passed container (or call a helper that accepts the container and *this). This avoids globals and keeps lifetime explicit.

  3. If you prefer a utility function, make SavePlayer a free or static function that accepts the container and a Player (or const Player&). Note: even as static/free, the container must still be provided to the function (you haven’t removed the need to pass manyPlayer into the scope that calls it).

Additional tips: always pass large objects or containers by reference to avoid copies and use const references when you don’t modify the argument. In modern C++ prefer emplace_back or std::move when appropriate to avoid unnecessary copies. Replace cin.sync() with cin.ignore() or line-oriented input (getline) for robust input handling. As suggested, use while/do-while for input validation loops. ’s static-function idea can work, but it still requires giving Choice access to the container; passing the container by reference is usually the cleanest fix.

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.