【发布时间】:2013-11-26 15:02:23
【问题描述】:
所以,我有两个类:Node 和 Graph。在 Graph 类的私有部分我声明:
int size;
Node* n;
在 Graph 构造函数中,我正在尝试创建一个动态数组:
size=1;
Node *n = new Node[size];
但我收到一个错误:“访问冲突读取位置 0xcccccd44”。我该如何解决?我知道我一定是对数组做错了,但我不知道要修复什么以及如何修复它。
类图:
class Graph {
friend class Node;
private:
int size;
Node* n;
public:
Graph();
Graph(int, Vertex*);
~Graph();
void Draw(RenderWindow &);
void Update(RenderWindow &, GameObject &, bool);
};
还有两个构造函数:
Graph::Graph() {
size=1;
Node *n = new Node[size];
}
Graph::Graph(int s, Vertex p[]) {
size=s;
Node *n = new Node[size];
for (int i=0; i<size; i++) {
n[i].setNumer(i);
n[i].setX(p[i].getX());
n[i].setY(p[i].getY());
}
}
【问题讨论】:
-
你需要发布更多代码,但我猜你没有关注the rule of three。
-
嗯,这里真的没有太多代码可以添加了...我将代码放在第一篇文章中。
-
你的意思是初始化成员
n,而不是声明一个同名的局部变量?为什么不使用std::vector<Node>来避免手动处理指针时遇到的所有问题? -
不应该是
n = new Node[size];,你隐藏了你的成员变量n。 -
无论如何都要使用
std::vector。