C++ C++;-列表没有保留其元素
我有两个班;TestUnit和TestShot。 TestUnit需要保存一个TestShot列表,但是当我稍后引用该列表时,我给它的所有元素都消失了 TestUnit.hC++ C++;-列表没有保留其元素,c++,list,C++,List,我有两个班;TestUnit和TestShot。 TestUnit需要保存一个TestShot列表,但是当我稍后引用该列表时,我给它的所有元素都消失了 TestUnit.h #include <string> #include <list> #include "TestShot.h" using namespace std; class TestUnit { public: TestUnit(string); string getName(void);
#include <string>
#include <list>
#include "TestShot.h"
using namespace std;
class TestUnit
{
public:
TestUnit(string);
string getName(void);
void addShot(TestShot);
list<TestShot> getShots(void);
bool operator == (const TestUnit& tu) const { return name == tu.name; }
bool operator != (const TestUnit& tu) const { return !operator==(tu); }
private:
string name;
list<TestShot> shots;
};
#include <string>
using namespace std;
class TestShot
{
public:
TestShot(string);
string getName(void);
private:
string name;
};
MAIN
#include <string>
#include "exports.h"
#include "TestUnit.h"
using namespace std;
// Global Variables
list<TestUnit> testUnits;
int main()
{
int nShots1 = 0;
int nShots2 = 0;
// Create Unit
TestUnit *testUnit = new TestUnit("Name");
testUnits.push_front(*testUnit);
// Create Shot and add to Unit with same 'name'
TestShot *testShot = new TestShot("Name");
for (TestUnit unit : testUnits)
{
if (unit.getName() == (*testShot).getName())
{
unit.addShot(*testShot);
nShots1 = unit.getShots().size();
}
}
// Display number of Shots for each Unit
for (TestUnit unit : testUnits)
{
nShots2 = unit.getShots().size();
std::cout << nShots1 << ", " << nShots2 << std::endl;
}
system("PAUSE");
};
因此,列表意识到,添加到列表中后,它会立即填充,但当我需要使用它时,它会变成空的。
我猜这是一个范围问题,但我似乎无法理解。
非常感谢您的帮助 在每个
for
循环中,您都是按值访问列表
元素,因此您实际上是在复制列表
中的内容,对其进行修改,然后将其销毁。将循环更改为如下所示:
for (TestUnit &unit : testUnits)
{
if (unit.getName() == (*testShot).getName())
{
unit.addShot(*testShot);
nShots1 = unit.getShots().size();
}
}
<> P>因为使用C++ 11或更高版本,还可以使用“代码>自动<代码>而不是显式类型(例如,<>代码> Auto.Ung:TestSubUs/COD>)。 <代码> GETHOSUTSH()/Cord>正在返回列表的副本,而不是对它的引用。因为C++不工作于java。复制对象实际上是复制对象。对不起,StackOverflow不是这样工作的。“这是我的一堆代码,请帮我调试一下”这样的问题被认为是离题的。请访问并阅读以了解更多信息。由于一个简单的排版错误,我很想投票关闭此操作,因为您没有通过引用进行迭代,因此只修改了循环中元素的一个副本。我认为这不是一个足够先进的问题,它是一个有用的问题。你也缺少删除操作数,不知道为什么你要用新的创建对象,然后传递值,猜测你来自java/C++学习如何在C++代码中工作……不要使用
new
。
#include <string>
#include "exports.h"
#include "TestUnit.h"
using namespace std;
// Global Variables
list<TestUnit> testUnits;
int main()
{
int nShots1 = 0;
int nShots2 = 0;
// Create Unit
TestUnit *testUnit = new TestUnit("Name");
testUnits.push_front(*testUnit);
// Create Shot and add to Unit with same 'name'
TestShot *testShot = new TestShot("Name");
for (TestUnit unit : testUnits)
{
if (unit.getName() == (*testShot).getName())
{
unit.addShot(*testShot);
nShots1 = unit.getShots().size();
}
}
// Display number of Shots for each Unit
for (TestUnit unit : testUnits)
{
nShots2 = unit.getShots().size();
std::cout << nShots1 << ", " << nShots2 << std::endl;
}
system("PAUSE");
};
1, 0
for (TestUnit &unit : testUnits)
{
if (unit.getName() == (*testShot).getName())
{
unit.addShot(*testShot);
nShots1 = unit.getShots().size();
}
}