我在这个网站上看到了一些解决方案,但是没有一个解决了我的问题。我正在实现一个n-子程序,不平衡的树类型和add操作给了我一个异常。守则如下:
struct Node {
// Just to initialize the parent node to zero, default constructor
Node(Node *parent = 0) : m_parent(parent) {
}
Node *m_parent;
vector<Node *> m_children;
GameBoard m_currentBoard;
};发生错误的地方:
Node *tempNode = 0;
// Going through each of them to create new nodes
for (unsigned int i = 0; i < availableBoards.size() ; i++) {
// Create a new node
tempNode = new Node;
tempNode->m_parent = curNode;
tempNode->m_currentBoard.setBoard(availableBoards[i]);
// This is the line when program crashes
curNode->m_children.push_back(tempNode);
}我也尝试过在循环中声明tempNode,但也没有帮助。
我查看了Visual的手表,curNode不是NULL,也不是tempNode。
我为什么要犯这个错误?
谢谢你的回答。
发布于 2014-01-22 13:30:57
问题是在调用第二个代码段之前就销毁了运行第二个代码段的对象。其内容如下:
class A{
int *getAsdf();
int *asdf;
}
int main() {
A *newObj = new A;
delete newObj;
newObj->getAsdf();
}对于如何从以前被删除的对象中调用方法,我一点也不知道。错误发生在像getAsdf这样的函数中,甚至参数都是有效的。有人能解释一下吗?
https://stackoverflow.com/questions/21283799
复制相似问题