简体   繁体   中英

Weird output with class getter function C++

So I have this piece of code for a darts game, in my header file:

    class Player
    {
    private:
        string playerName;
        int bullAccuracy;
        int outerAccuracy;
        int singleAccuracy;
    public:

        //getters
        string& getName();
        int& getBullAccuracy();
        int& getSingleAccuracy();

        //setters
        void setName(string& name);
        void setBullAccuracy(int& bull_accuracy);
        void setSingleAccuracy(int& single_accuracy);
    };

And then the correspondent getter and setter functions in the .cpp file look like this:

    //getters
    string& Player::getName()
    {
        return playerName;
    }
    unsigned int& Player::getScore()
    {
        return playerScore;
    }
    int& Player::getBullAccuracy()
    {
        return bullAccuracy;
    }
    int& Player::getSingleAccuracy() 
    {
        return singleAccuracy;
    }

//setters
void Player::setName(string& name)
{
    playerName = name;
}
void Player::setBullAccuracy(int& bull_accuracy)
{
    bullAccuracy = bull_accuracy;
}
void Player::setSingleAccuracy(int& single_accuracy)
{
    singleAccuracy = single_accuracy;
}

All of this very simple and basic getter/setter code for a class data memebers, right?

In the main() function though, I set the names for my players, their various accuracies, and then display them throught the getters. All of these are done using vectors of objects and iterators, like this:

vector<Player> player(2);
vector<Player>::iterator modIter;
int bAccuracy, sAccuracy;
string playerName;
cout << "Input the name of the players: " << endl;
for (modIter = player.begin(); modIter != player.end(); modIter++)
{
    cin >> playerName;
    modIter->setName(playerName);
}
cout << "\nSet the players' bull accuracy:" << endl;
for (modIter = player.begin(); modIter != player.end(); modIter++)
{
    cout << modIter->getName() << ": ";
    cin >> bAccuracy;
    modIter->setBullAccuracy(bAccuracy);
}
cout << "\nSet the players' single hit accuracy: " << endl;
for (modIter = player.begin(); modIter != player.end(); modIter++)
{
    cout << modIter->getName() << ": ";
    cin >> sAccuracy;
    modIter->setBullAccuracy(sAccuracy);
}
//display values to check program working well so far
for (modIter = player.begin(); modIter != player.end(); modIter++)
{
    cout << modIter->getBullAccuracy() << endl;
    cout << modIter->getOuterAccuracy() << endl;
    cout << modIter->getSingleAccuracy() << endl;
}

And after running the code, and typing in input "Joe" and "Sid" for the names, 71 and 73 respectively for bull accuracy, and then 80 for both players for the single hit accuracy, this is what my console outputs:

Input the name of the players:

Joe

Sid

Set the player's bull accuracy:

Joe: 71

Sid: 73 //this means that at least the names were set correctly

Set the player's single hit accuracy:

Joe: 80

Sid: 80

//and here is the weird output

80 //this is supposed to be 71

-842150451 //and this 80

80 //here the 73

-842150451 //and here again 80

So what exactly happened because this is really basic and familiar use of getters and setters, and I have no idea how to debug this by myself since I don't see the issue. Is there something in my code that I can't see that's altering the results?

Thank you very much in advance for your help

When you prompt for single accuracy, you're still calling setBullAccuracy method, not setSingleAccuracy . You also didn't initialize your member variables which is why printing out singleAccuracy is giving you garbage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM