小编roc*_*645的帖子

在Vector中正确存储对象

我是C++的新手,这是我第一次发帖(灾难的秘诀).我花了大约一天时间试图解决我的问题/问题,同时在论坛上找不到容易识别的解决方案.我的问题可能已被提出或者已经发布了解决方案,但我忽略了它或误解了它.这里有类似的帖子,但对我来说,如何处理这些信息并不明显.

我将提供有关此代码的简明扼要的高级摘要.然后我会问我无法找到答案的具体问题,并且我将按照我编写的代码进行操作.

摘要:我正在创建一个程序来帮助为游戏进行簿记.游戏可以具有任意数量的玩家,并且每个玩家具有作为玩家类/对象的元素的小属性/成员列表(例如,玩家名称,玩家抵抗等).首先要求用户输入每个玩家的名称(enteredName),程序必须为输入的每个名称创建一个新的Player对象.这似乎是由动态数组适当处理的,所以我选择使用向量(称为playerIndex)来存储每个Player对象.for循环允许用户输入名称,每个名称使用vector :: push_back实例化一个新的Player对象,该对象将被存储(复制?)到playerIndex中.在for循环结束时,用户应该留下一个Player对象的向量,每个Player对象在其playerName成员中存储一个名称.

问题/问题:在监视上述for循环中的向量时,代码似乎正常工作.在用户输入第N个玩家的名字之后,程序立即使用Player类函数getPlayerName()[实际代码:playerIndex [playerCounter] .getPlayerName()]吐出存储在playerIndex的第N个元素中的playerName字符串.一旦用户输入空白的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 …
Run Code Online (Sandbox Code Playgroud)

c++ vector object

1
推荐指数
1
解决办法
567
查看次数

标签 统计

c++ ×1

object ×1

vector ×1