C++指针向量,传递次数过多?
我几个小时以来一直在试图找出如何解决这个段错误问题。
我有一个用 C++ 编写的纸牌游戏,有四个玩家。玩家(基类)可以是计算机,也可以是人类(派生类)。为了存储它们,我有另一个名为playerContainer的类,它有自己的函数并使用玩家指针的私有成员向量。
std::vector<Player*> players_;
当我向向量中添加一个新玩家时,我会执行以下操作:
Human *newPlayer = new Human();
container.add(newPlayer);
其中 add 是:
void playerContainer::add(Player* newPlayer) {
players_.push_back(newPlayer);
}
然后我有一个名为 gameLogic 的类,它有一个私有成员playerContainer 并执行玩家初始化(以及其他一些操作)。
然而,在尝试将其合并到 MVC 实现中后,我遇到了段错误。
在 GUI 上,单击按钮即可开始游戏。该按钮调用窗口的 startGame 函数,该函数收集参数,然后调用其私有成员 Model 类的 startGame,该函数随后调用其私有成员 gameLogic 的initializePlayer 函数,该函数随后调用其私有成员playerContainer 的 add 函数。到那时程序就会中断。
经过一些测试,我发现如果我保存了一个步骤,就不会出现段错误。例如,如果我给窗口对象一个gameLogic对象,然后调用它的initializePlayer函数等等,程序将正常进行,不会出现错误。
我确信有一个比组合 Model 和 gameLogic 类更好的解决方案,因为它们已经相当臃肿了。有人可以提供一些见解吗?
另外,如果这有点令人困惑,请让我澄清。我尽力解释清楚,但涉及到这么多不同的类,很难做到这一点。
编辑:无法尝试用 int 替换 Player* ,代码对于我来说太大了,无法及时完成此操作。
我已经尝试过 valgrind,但日志很大并且通常没有帮助。或者至少,我无法理解其中的大部分内容。对于那些想看的人,这里有一个抄写链接: http://www.scribd.com/doc/60210916/Memory-Leak
seg 错误是在playerContainer 的add 函数的push_back 行上引发的。
我会尝试调试器...
I've been trying to figure out how to solve this seg-fault problem for hours now.
I have a card game in C++, four players. A player (base class) can either be a computer or a human (derived classes). To store them, I have another class called playerContainer, which has its own functions and uses a private member vector of player pointers.
std::vector<Player*> players_;
When I add a new player to the vector, I do something like this:
Human *newPlayer = new Human();
container.add(newPlayer);
Where add is:
void playerContainer::add(Player* newPlayer) {
players_.push_back(newPlayer);
}
And then I have one class called gameLogic which has a private member playerContainer and performs the player initialization (among several other things).
However, I'm getting seg faults after trying to make incorporate it into an MVC implementation.
On the GUI, you click a button to start the game. The button calls the window's startGame function, which gathers parameters and then calls its private member Model class's startGame, which then calls its private member gameLogic's initializePlayer function, which then calls its private member playerContainer's add function. At that point the program will break.
On a bit of testing, I found that it would not seg fault if I saved a step. For example, if I gave the window object a gameLogic object, and then called its initializePlayer function and so forth, the program would proceed normally without error.
I'm certain there's a better solution than to combine the Model and gameLogic classes as they are already quite bloated. May anyone provide some insight?
Also, if this is any bit confusing, please ask me to clarify. I did my best to explain clearly, but with so many different classes involved, it gets difficult to do so.
EDIT: Can't try replacing Player* with int, the code's way too huge for me to be able to do this in any timely fashion.
I HAVE tried valgrind, but the log was huge and generally unhelpful. Or at the very least, I couldn't understand much of it. For those who want to take a look, here's a scribd link:
http://www.scribd.com/doc/60210916/Memory-Leak
The seg fault is being raised on the playerContainer's add function, on the push_back line.
I'll try my hand at the debugger...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您的模型在某个地方,但在调用initializePlayer之前,实际上分配了playerContainer吗?
Are you sure your Model, somewhere, but before initializePlayer is called, actually allocates the playerContainer?