gpt4 book ai didi

c++ - 'main'已停止工作-C++ [dev c++]

转载 作者:行者123 更新时间:2023-12-03 17:49:00 24 4
gpt4 key购买 nike

我的代码表现得很奇怪。有时它会工作,而在其他时间会崩溃。

当它崩溃时说:

a problem caused the program to stop working correctly



我的 main:
int main() {
start();
Read();

cout << "Enter the code of the course: ";
cin >> coursecode;
cout << "\n1111111\n";
searchCourse(coursecode);
cout << "\n222222\n";

return 0;
}

我在searchCourse函数的上方和下方编写了两个提示,以查看程序是否编译了所有行。它确实可以编译所有内容,最后在崩溃前会打印222222。

start方法仅创建BinaryTree对象的数组,然后存储
根据 class 的学生数据(从文本文件中读取)。

开始():
BinaryTree *a[10];

void start()
{
for(int g=1;g<=10;g++)
{
a[g] = new BinaryTree(g);
}
}

searchCourse():
void searchCourse(string code)
{
for(int j=1;j<=count;j++)
{
if(a[j]->checkit(code)!=0)
{
a[j]->display();

break;
}
}
}

BinaryTree.h中的Checkit():
bool checkit(string m)
{
if (root==NULL)
return false;
else
if(root->data.getCourse()==m)
return true;
else
return false;
}

最佳答案

BinaryTree *a[10];
for(int g=1;g<=10;g++)
{
a[g] = new BinaryTree(g);
}

会有内存异常。您有一个10的数组,并且您正在尝试访问第11个元素(因为直到 g<=10,而 a[10]是第11个元素)。用:
for(int g=0;g<10;g++)

代替。如果Binary Tree从1开始,您可能还必须执行 new BinaryTree(g+1);

这也是代码中其他位置的错误,例如 for(int j=1;j<=count;j++)( for(int j=0;j<count;j++)可能就是您想要的)。

数组从0开始。 Why does the indexing start with zero in 'C'?

关于c++ - 'main'已停止工作-C++ [dev c++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47895686/

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