'main' 已停止工作 - C++ [dev c++]
'main' has stopped working - C++ [dev++]
我的代码表现得很奇怪。它有时有效,有时会崩溃。
当它崩溃时它说:
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 函数的上方和下方写了两个 cout,以查看程序是否编译了所有行。它确实编译了所有东西,最后它在崩溃前打印了 222222。
start 方法只是创建一个 BinaryTree 对象数组,然后存储
根据课程的学生数据(从文本文件中读取)。
开始():
BinaryTree *a[10];
void start()
{
for(int g=1;g<=10;g++)
{
a[g] = new BinaryTree(g);
}
}
搜索课程():
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]
是第十一个元素)。使用:
for(int g=0;g<10;g++)
相反。如果二叉树从 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'?
我的代码表现得很奇怪。它有时有效,有时会崩溃。
当它崩溃时它说:
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 函数的上方和下方写了两个 cout,以查看程序是否编译了所有行。它确实编译了所有东西,最后它在崩溃前打印了 222222。
start 方法只是创建一个 BinaryTree 对象数组,然后存储 根据课程的学生数据(从文本文件中读取)。
开始():
BinaryTree *a[10];
void start()
{
for(int g=1;g<=10;g++)
{
a[g] = new BinaryTree(g);
}
}
搜索课程():
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]
是第十一个元素)。使用:
for(int g=0;g<10;g++)
相反。如果二叉树从 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'?