C++ C++;main()末尾关于错误处理的错误 void main(){ 试一试{ GameEntry deletedItem=newList.remove(13); } 捕获(IndexAutofBound&e){ cout

C++ C++;main()末尾关于错误处理的错误 void main(){ 试一试{ GameEntry deletedItem=newList.remove(13); } 捕获(IndexAutofBound&e){ cout,c++,exception-handling,C++,Exception Handling,处理此错误与main的返回类型无关。它应该始终是int main() return和exit()之间的区别: 返回为作用域变量调用析构函数 exit()除静态变量外,不调用析构函数 因此,您应该小心地使用exit() 我知道如何通过在cout之后放置exit(0)来修复此错误,您必须始终使用int main。是的,您必须使用int main(…),但这不会导致崩溃(我猜是崩溃吗?)。如果没有MCVE,则不知道崩溃原因是什么。在一个或多个GameEntry,IndexOutOfBound,以及

处理此错误与main的返回类型无关。它应该始终是
int main()

return
exit()
之间的区别:

  • 返回
    为作用域变量调用析构函数
  • exit()
    除静态变量外,不调用析构函数
因此,您应该小心地使用
exit()


我知道如何通过在cout之后放置exit(0)来修复此错误,您必须始终使用
int main
。是的,您必须使用
int main(…)
,但这不会导致崩溃(我猜是崩溃吗?)。如果没有MCVE,则不知道崩溃原因是什么。在一个或多个
GameEntry
IndexOutOfBound
,以及任何类型的
newList
中都有一个或多个错误。我发现类的析构函数中有一个错误。“任何其他main声明都会导致程序定义错误。”-不这样认为-可能是实现定义的。当然,独立的实现或多或少可以做他们喜欢做的事情。@NeilButterworth修复:我知道最好不要反驳你的知识。我发现类的析构函数中有一个错误。很抱歉没有发布完整的源代码。
void main(){
    try {
        GameEntry deletedItem = newList.remove(13);
    }
    catch (IndexOutOfBound &e) {
        cout << e.what() << endl;
    }
    cout << ":)";
}
void main(){

    // You have some variables declared here.
    // That get destroyed when main() exists (ie there destructors run).
    // This is where you bug is.
    // calling exit() after the last line below will hide this error
    // as the destructor for these objects will not be run.

    try {
        GameEntry deletedItem = newList.remove(13);
    }
    catch (IndexOutOfBound &e) {
        cout << e.what() << endl;
    }
    cout << ":)";
}
int main()
int main(int argc, char* argv[])  // or equivalent types.