C++对象不保存值
C++ Object doesn't save value
我为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;
这些是静态字段吗?如果是,问题是你只存储你创建的最后一个玩家的名字和姓氏(名字?
相关文章:
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- C++17 如何保存泛型可调用对象以供以后使用
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 将对象信息保存到二进制文件中
- C++两次从文件保存对象读取多重继承
- 有没有办法自动初始化 std::shared_ptr 来保存有效对象?
- 如何调用返回双精度或字符串并将其保存在地图上但作为对象的类的方法
- 将抽象值对象保存在包装器中和继承的用法中
- 如何保存模板的对象阵列进行文件
- 我的共享内存对象保存在哪里
- 保存对象的攻击函数并稍后称呼它是明确定义的行为
- 如何创建一个动态数组,该数组可以保存从C++中派生的同一基类的所有不同对象
- 即使在 C++ 中的 fstream.clear() 之后,对象也没有被保存
- 分类一个向量,该向量由该类中的成员字符串保存对象
- C 保存对象分配和删除
- 保存对象并以任何特定顺序加载它们
- 访问 C 中保存C++对象的 void 指针的内容
- 如何在单个数组中保存对象的动态数组
- 如何保存对象(在这种情况下,球员)在一个列表中的c++
- 如何保存对象的集合并在c++向量中使用它们