gpt4 book ai didi

c++ - 使用大小为 4 的未初始化值

转载 作者:行者123 更新时间:2023-11-30 00:57:17 24 4
gpt4 key购买 nike

编辑:我当时很蠢,我 - 没有 - 分配它。再次感谢您的帮助!

这是一个我以为自己已经结束的错误,但它又回来了,我不知道为什么会这样。

这是导致错误的 .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 -> myNumber = -1;

“theWALL”和“safety”都是私有(private) *Room 对象:

Private:
Room * theWALL;
Room * safety;

“房间”结构如下所示:

struct Room
{
bool visited;
int myNumber;

Room *North;
Room *East;
Room *South;
Room *West;
};

当我调用 new Graph( numRooms ) 时它被初始化,我正在填充它的信息......但我得到这个错误。

最佳答案

看起来你没有分配

Room * theWALL;

在使用之前

theWALL -> myNumber = -1;

尝试类似的东西

theWall = new Room();
theWALL -> myNumber = -1;

你有同样的错误:

 Room * safety;

也尝试分配它。

关于c++ - 使用大小为 4 的未初始化值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8462265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com