这是一个我认为自己已经结束的错误,但是它又回到了它,我不知道它为什么会发生。
以下是导致错误的.C文件中的代码:
int main(int argc, char* argv[])
{
if( argc > 3 )
{
cout << "Too many arguments. Program will close.\n";
return 1;
}
else
{
int numRooms = 0;
int start = 0;
ifstream file;
if( argc == 3 )//Dump wanted
file.open( argv[ 2 ] , ifstream::in );
else
file.open( argv[ 1 ] , ifstream::in );
if( file.is_open() )
{
file >> numRooms;
Graph * maze = new Graph( numRooms );
Graph * maze = new Graph(numRooms); 是引用错误的地方(a11.C:41):
==17900== Use of uninitialised value of size 4
==17900== at 0x8048F05: Graph::Graph(int) (Graph.C:25)
==17900== by 0x8048C1D: main (a11.C:41)
从这里我们深入研究Graph.C文件,第16 - 30行:
Graph::Graph( int num )
{
numRooms = num;
_index = 0;
_start = 0;
easyDelete = new Room*[ num ];
escapePath = new string[ num ];
theWALL -> myNumber = -1;
theWALL -> visited = true;
safety -> myNumber = 0;
safety -> visited = false;
}
第25行是这样的: theWALL - &gt; myNumber = -1;
“theWALL”和“安全”都是私人的*房间对象:
Private:
Room * theWALL;
Room * safety;
结构“房间”看起来像这样:
struct Room
{
bool visited;
int myNumber;
Room *North;
Room *East;
Room *South;
Room *West;
};
当我调用新的Graph(numRooms)时它被初始化了,我正在填写它的信息......但是我收到了这个错误。
答案 0 :(得分:6)
看起来你没有分配
Room * theWALL;
之前使用
theWALL -> myNumber = -1;
尝试类似
的内容theWall = new Room();
theWALL -> myNumber = -1;
你有同样的错误:
Room * safety;
尝试分配它。