简体   繁体   English

带有类getter函数C ++的奇怪输出

[英]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: 然后,.cpp文件中的对应getter和setter函数如下所示:

    //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. 不过,在main()函数中,我设置了球员的姓名,他们的各种准确性,然后在吸气剂中显示他们。 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: 在运行代码之后,输入输入“ Joe”和“ Sid”作为名称,分别为71和73表示公牛精度,然后为两个玩家输入80的单次命中精度,这就是我的控制台输出的内容:

Input the name of the players: 输入玩家名称:

Joe

Sid 席德

Set the player's bull accuracy: 设置玩家的公牛准确性:

Joe: 71 乔71

Sid: 73 //this means that at least the names were set correctly Sid:73 //这意味着至少名称设置正确

Set the player's single hit accuracy: 设置玩家的单次命中精度:

Joe: 80 乔:80

Sid: 80 席德:80

//and here is the weird output //这是奇怪的输出

80 //this is supposed to be 71 80 //应该是71

-842150451 //and this 80 -842150451 //和这80

80 //here the 73 80 //这里73

-842150451 //and here again 80 -842150451 //这里又是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. 所以发生了什么事,因为这实际上是对getter和setter的基本和熟悉的使用,而且我不知道如何自己调试它,因为我看不到问题。 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 . 当提示输入单精度时,您仍在调用setBullAccuracy方法,而不是setSingleAccuracy You also didn't initialize your member variables which is why printing out singleAccuracy is giving you garbage. 您也没有初始化成员变量,这就是为什么打印出singleAccuracy会给您带来垃圾的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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