Mut*_*han 1 c++ compiler-errors try-catch
在使用 try-catch 时遇到了这个错误。但我无法查出这个错误的原因,尽管我上网冲浪,所以。
\n\n我的代码是...
\n\nint main()\n{\nQueue q;\nint choice,data;\n\n while(1)\n {\n choice = getUserOption();\n switch(choice)\n {\n case 1:\n cout<<"\\nEnter an element:";\n cin>>data;\n q.enqueue(data);\n break;\n case 2:\n int element;\n element = q.dequeue();\n cout<<"Element Dequeued:\\n"<<element;\n break;\n case 3:\n q.view();\n break;\n case 4:\n exit(EXIT_SUCCESS);\n }\n catch(UnderFlowException e)\n {\n cout<<e.display();\n }\n catch(OverFlowException e)\n {\n cout<<e.display();\n }\n\n }// end of while(1)\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n对我来说,上面代码中的所有内容似乎都是正确的。但是 g++ 编译器正在抛出......
\n\nmuthu@muthu-G31M-ES2L:~/LangFiles/cppfiles/2ndYearLabException$ g++ ExceptionHandlingEdited.cpp\nExceptionHandlingEdited.cpp: In member function \xe2\x80\x98void Queue::enqueue(int)\xe2\x80\x99:\nExceptionHandlingEdited.cpp:89:97: warning: deprecated conversion from string constant to \xe2\x80\x98char*\xe2\x80\x99\nExceptionHandlingEdited.cpp: In member function \xe2\x80\x98int Queue::dequeue()\xe2\x80\x99:\nExceptionHandlingEdited.cpp:113:95: warning: deprecated conversion from string constant to \xe2\x80\x98char*\xe2\x80\x99\nExceptionHandlingEdited.cpp: In member function \xe2\x80\x98void Queue::view()\xe2\x80\x99:\nExceptionHandlingEdited.cpp:140:66: warning: deprecated conversion from string constant to \xe2\x80\x98char*\xe2\x80\x99\nExceptionHandlingEdited.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\nExceptionHandlingEdited.cpp:185:3: error: expected primary-expression before \xe2\x80\x98catch\xe2\x80\x99\nExceptionHandlingEdited.cpp:185:3: error: expected \xe2\x80\x98;\xe2\x80\x99 before \xe2\x80\x98catch\xe2\x80\x99\nExceptionHandlingEdited.cpp:189:3: error: expected primary-expression before \xe2\x80\x98catch\xe2\x80\x99\nExceptionHandlingEdited.cpp:189:3: error: expected \xe2\x80\x98;\xe2\x80\x99 before \xe2\x80\x98catch\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n
catch没有 就不可能有try。把线:
try {
Run Code Online (Sandbox Code Playgroud)
在你while发言之前。
如果您想摆脱有关字符串常量的警告,您可能必须将类型更改为const char *或显式转换/复制它们。或者您可以使用该-Wno-write-strings选项gcc。