我是C ++的新手,这是我第一次发帖(灾难的秘诀)。我花了大约一天时间试图解决我的问题/问题,同时在论坛上找不到容易识别的解决方案。可能我的问题已被提出或者已经发布了解决方案,但我忽略了它或误解了它。类似的帖子存在here,但我对这些信息的处理方式并不明显。
我将提供有关此代码的简明扼要的高级摘要。然后,我会问我无法找到答案的具体问题,并且我将按照我编写的代码进行操作。
总结:我正在创建一个程序来帮助对游戏进行记账。游戏可以具有任意数量的玩家,并且每个玩家具有作为玩家类/对象的元素的属性/成员的小列表(例如,玩家名称,玩家抵抗等)。首先要求用户输入每个玩家的名称(enteredName),并且程序必须为输入的每个名称创建一个新的Player对象。这似乎是由动态数组适当处理的,所以我选择使用向量(称为playerIndex)来存储每个Player对象。 for循环允许用户输入名称,每个名称使用vector :: push_back实例化一个新的Player对象,该对象将被存储(复制?)到playerIndex中。在for循环结束时,用户应该留下一个Player对象的向量,每个Player对象在其playerName成员中存储一个名称。
问题/问题:在监视上述for循环中的向量时,代码似乎正常工作。在用户输入第N个玩家的名字之后,程序立即使用Player类函数getPlayerName()[实际代码:playerIndex [playerCounter] .getPlayerName()]吐出存储在playerIndex的第N个元素中的playerName字符串。一旦用户输入空白播放器名称(即按下输入而不输入名称),它就表示用户已输入所有播放器名称,因此for循环终止。在此循环之后,设计用于输出存储在playerIndex中的每个Player对象的playerName的循环不会输出预期的名称。我不知道为什么会发生这种情况,但基于我对构造函数的极少知识,我猜测它与Player类的复制或移动构造函数有关。谁能清楚地解释如何处理这个问题?我担心我可能犯了一个可怜的愚蠢,新手的错误和/或误解了C ++的一个关键概念。
代码:此代码被裁剪/简化为尽可能清晰。例如,Player类显示只有一个成员(playerName),而在原始代码中,它有四个或五个其他成员。
//HeaderPlayerClass.hpp
#include <iostream>
#include <string>
#ifndef PLAYERCLASS_HPP
#define PLAYERCLASS_HPP
using std::string;
class Player {
private:
string *playerName;
public:
Player();
Player(string);
~Player();
string getPlayerName();
};
#endif
//PlayerClass.cpp
#include "HeaderPlayerClass.hpp"
#include <iostream>
#include <string>
using std::string;
Player::Player() {
playerName = new string;
}
Player::Player(string enteredName) {
playerName = new string;
*playerName = enteredName;
}
Player::~Player() {
delete playerName;
}
string Player::getPlayerName() {
return *playerName;
}
//main.cpp
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include "HeaderPlayerClass.hpp"
using std::cin;
using std::cout;
using std::string;
using std::vector;
int main(int argc, char** argv) {
string buffer;
vector<Player> playerIndex;
int playerCounter = 0;
for(;;) {
if(playerCounter==0) {
cout << "\nEnter player name and press enter; leave blank and press enter to continue.\n";
}
cout << "\nPlayer " << playerCounter+1 << ":";
getline(cin, buffer);
if(buffer == "esc") {
cout << "PROGRAM EXITED BY USER\n";
return 0;
}
if(buffer.empty()) {
break;
}
playerIndex.push_back(Player(buffer));
cout << "Player " << playerCounter+1 << "'s name:" << playerIndex[playerCounter].getPlayerName() << "\n";
++playerCounter;
}
for(int ii = 0 ; ii < playerIndex.size() ; ii++) {
cout << "\nThis should display player " << ii+1 << "'s name:" << playerIndex[ii].getPlayerName();
}
return 0;
}
答案 0 :(得分:5)
class Player {
private:
string *playerName;
不要存储指向字符串的指针,存储字符串本身
class Player {
private:
string playerName;
构造
Player::Player() {
playerName = new string;
}
如果您存储字符串本身,则不需要这样做 - 默认构造函数会为您初始化它。
这是问题开始的地方 - 这会让你容易受到内存泄漏的影响,除非你仔细编写析构函数
Player::Player(string enteredName) {
playerName = new string;
*playerName = enteredName;
}
如果您存储字符串本身,您只需要:
Player::Player( const string& enteredName)
: playerName ( enteredName )
{}
你的构造函数真的很吓人。只需在存储字符串本身时将内容保留为默认析构函数
Player::~Player() {
delete playerName;
}
接下来,你正在努力保持自己的计数器。 std :: vector维护自己的计数并使用
playerIndex.size()
将节省麻烦和错误