C++对象不保存值

C++ Object doesn't save value

本文关键字:保存 对象 C++      更新时间:2023-10-16

我为Player类创建的对象不会更改私有变量,即使它在更改它的函数中也是如此。main()函数中使用的对象可能存在一些参考问题,但我无法弄清楚在哪里或为什么。

具体来说,主文件末尾的循环打印玩家在向量Players中赢得的比赛,但所有分数都返回 0。而当播放器object.matchWon()函数正确解析和打印分数并显示对私有变量所做的更改时。

我怀疑在主代码中使用getPlayer()函数存在问题。

提前感谢!!

Player.cpp

#include "Player.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
//constructor
Player::Player(string first, string last) {
    Player::first = first;
    Player::last = last;
    gWins = 0;
    gLoss = 0;
    mWins = 10;
    mLoss = 2;
}
//setters
void Player::addLoss(int increment) {
    this->gLoss+=increment;
}
void Player::addWins(int increment) {
    this->gWins+=increment;
}
void Player::matchWon(vector<string> scores) {
    for (int i = 5;i<scores.size()-1;i++){
        cout<<(int)scores[i][0]-'0'<<"-";
        cout<<(int)scores[i][2]-'0'<<endl;
        gWins+=scores[i][0]-'0'; //add games won
        cout<<gWins<<endl;
        gLoss+=(int)scores[i][2]-'0';   //add games lost
    }
    this->mWins++;
}
void Player::matchLost(vector<string> scores) {
    this->mLoss++;
}
double Player::winPercentage() {
    return (double)mWins / mLoss;
}
//accessors
string Player::getFirstname() {
    return this->first;
}
string Player::getLastname() {
    return this->last;
}
int Player::getGameswon() {
    //cout<<gWins;
    return this->gWins;
}

main.cpp

#include <iostream>
#include "player.h"
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
vector<string> split(const string &s) {
    vector<string> elems;
    istringstream iss(s);
    do
    {
        string sub;
        iss >> sub;
        elems.push_back(sub);
        //cout << "Substring: " << sub << endl;
    } while (iss);
    return elems;
}
Player &getPlayer(vector<Player> &players, const string &first, const string &last){
    for (int i=0;i<players.size();i++){
        if (players[i].getFirstname()==first&&players[i].getLastname()==last){
            return players[i];
        }
    }
    players.push_back(Player(first,last));
    return (players[players.size()-1]);
}
int main(int argc, char *argv[]) {
    ifstream file(argv[1]);
    ofstream ofile(argv[2]);
    if (!file.is_open()){
        cerr <<"Could not open filen";
        return 0;
    }
    string line;
    vector<Player> players;
    while (getline(file,line).good()){
        vector<string> lineParsed = split(line);
        vector<string> matchData = split(line);
        Player p1 = getPlayer(players,lineParsed[0],lineParsed[1]);
        Player p2 = getPlayer(players,lineParsed[3],lineParsed[4]);
        p1.matchWon(lineParsed);
        cout<<p1.getFirstname()<<"!"<<p1.getGameswon()<<endl;
    }
    for (int i=0;i<players.size();i++){
        //cout<<players.size();
        cout<<players[i].getFirstname()<<":"<<players[i].getGameswon()<<endl;
    }

    return 0;
}

您正在复制您的播放器:

Player p1 = getPlayer(players,lineParsed[0],lineParsed[1]);
Player p2 = getPlayer(players,lineParsed[3],lineParsed[4]);

因此,当您p1.matchWon() 时,它发生在本地Player对象上,而不是向量中的对象。

尽管getplayer()的返回类型是 Player& ,但如果您将其分配给非引用变量,这并不重要。如果要修改向量中的Player实例,则应具有

Player& p1 = getPlayer(players,lineParsed[0],lineParsed[1]);
Player& p2 = getPlayer(players,lineParsed[3],lineParsed[4]);

顺便说一句,getPlayer()不安全。当您push_back矢量时,它可能会自行调整大小,从而使引用无效。您可能希望存储指针向量(或者只是确保通过例如 resize()矢量肯定有空间容纳您想要推送的所有玩家)。

如果包含类定义会有所帮助。似乎有什么奇怪的事情正在发生:

Player::first = first;
Player::last = last;

这些是静态字段吗?如果是,问题是你只存储你创建的最后一个玩家的名字和姓氏(名字?