代码中的这个错误是什么?

发布于 2024-12-06 19:10:52 字数 1844 浏览 2 评论 0原文

在使用 try-catch 时遇到了这个错误。但我无法查出这个错误的原因,尽管我上网冲浪 所以

我的代码是......

int main()
{
Queue q;
int choice,data;

 while(1)
 {
  choice  = getUserOption();
  switch(choice)
  {
   case 1:
     cout<<"\nEnter an element:";
     cin>>data;
     q.enqueue(data);
     break;
   case 2:
     int element;
     element = q.dequeue();
     cout<<"Element Dequeued:\n"<<element;
     break;
   case 3:
     q.view();
     break;
   case 4:
     exit(EXIT_SUCCESS);
  }
  catch(UnderFlowException e)
  {
   cout<<e.display();
  }
  catch(OverFlowException e)
  {
   cout<<e.display();
  }

 }// end of while(1)

       return 0;
}

对我来说,上面代码中的所有内容似乎都是正确的。但是 g++ 编译器正在抛出......

muthu@muthu-G31M-ES2L:~/LangFiles/cppfiles/2ndYearLabException$ g++ ExceptionHandlingEdited.cpp
ExceptionHandlingEdited.cpp: In member function ‘void Queue::enqueue(int)’:
ExceptionHandlingEdited.cpp:89:97: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In member function ‘int Queue::dequeue()’:
ExceptionHandlingEdited.cpp:113:95: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In member function ‘void Queue::view()’:
ExceptionHandlingEdited.cpp:140:66: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In function ‘int main()’:
ExceptionHandlingEdited.cpp:185:3: error: expected primary-expression before ‘catch’
ExceptionHandlingEdited.cpp:185:3: error: expected ‘;’ before ‘catch’
ExceptionHandlingEdited.cpp:189:3: error: expected primary-expression before ‘catch’
ExceptionHandlingEdited.cpp:189:3: error: expected ‘;’ before ‘catch’

while working in try-catch in came across this error. But I cant trace out the reason for this error though I surfed the net and SO.

My code is...

int main()
{
Queue q;
int choice,data;

 while(1)
 {
  choice  = getUserOption();
  switch(choice)
  {
   case 1:
     cout<<"\nEnter an element:";
     cin>>data;
     q.enqueue(data);
     break;
   case 2:
     int element;
     element = q.dequeue();
     cout<<"Element Dequeued:\n"<<element;
     break;
   case 3:
     q.view();
     break;
   case 4:
     exit(EXIT_SUCCESS);
  }
  catch(UnderFlowException e)
  {
   cout<<e.display();
  }
  catch(OverFlowException e)
  {
   cout<<e.display();
  }

 }// end of while(1)

       return 0;
}

For me everything in the above code seems to be correct. But g++ complier is throwing...

muthu@muthu-G31M-ES2L:~/LangFiles/cppfiles/2ndYearLabException$ g++ ExceptionHandlingEdited.cpp
ExceptionHandlingEdited.cpp: In member function ‘void Queue::enqueue(int)’:
ExceptionHandlingEdited.cpp:89:97: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In member function ‘int Queue::dequeue()’:
ExceptionHandlingEdited.cpp:113:95: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In member function ‘void Queue::view()’:
ExceptionHandlingEdited.cpp:140:66: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In function ‘int main()’:
ExceptionHandlingEdited.cpp:185:3: error: expected primary-expression before ‘catch’
ExceptionHandlingEdited.cpp:185:3: error: expected ‘;’ before ‘catch’
ExceptionHandlingEdited.cpp:189:3: error: expected primary-expression before ‘catch’
ExceptionHandlingEdited.cpp:189:3: error: expected ‘;’ before ‘catch’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2024-12-13 19:10:52

如果没有 try,就不可能有 catch。将行:

try {

放在 while 语句之前。

如果您想消除有关字符串常量的警告,您可能必须将类型更改为 const char * 或显式转换/复制它们。或者您可以使用 gcc-Wno-write-strings 选项。

You can't have a catch without a try. Put the line:

try {

before your while statement.

If you want to get rid of the warnings about the string constants, you'll probably have to change the types to const char * or explicitly cast/copy them. Or you can use the -Wno-write-strings option to gcc.

眼角的笑意。 2024-12-13 19:10:52

您需要将代码包含在 try {...} 构造中,否则 catch 将不知道它应该捕获什么代码。

while 循环包装到 try 中:

try {
 while(1)
   {
    .....
   }// end of while(1)
} catch(UnderFlowException e) ...

参考

You need to surround your code in try {...} construct, otherwise catch will not know what code it should catch.

Wrap your while loop into try:

try {
 while(1)
   {
    .....
   }// end of while(1)
} catch(UnderFlowException e) ...

Reference.

电影里的梦 2024-12-13 19:10:52

试试这个:

int main()
{
Queue q;
int choice,data;

 while(1)
 {
  choice  = getUserOption();
  try 
  {
    switch(choice)
    {
     case 1:
       cout<<"\nEnter an element:";
       cin>>data;
       q.enqueue(data);
       break;
     case 2:
       int element;
       element = q.dequeue();
       cout<<"Element Dequeued:\n"<<element;
       break;
     case 3:
       q.view();
       break;
     case 4:
       exit(EXIT_SUCCESS);
    }
  }
  catch(UnderFlowException e)
  {
   cout<<e.display();
  }
  catch(OverFlowException e)
  {
   cout<<e.display();
  }

 }// end of while(1)

       return 0;
}

Try this:

int main()
{
Queue q;
int choice,data;

 while(1)
 {
  choice  = getUserOption();
  try 
  {
    switch(choice)
    {
     case 1:
       cout<<"\nEnter an element:";
       cin>>data;
       q.enqueue(data);
       break;
     case 2:
       int element;
       element = q.dequeue();
       cout<<"Element Dequeued:\n"<<element;
       break;
     case 3:
       q.view();
       break;
     case 4:
       exit(EXIT_SUCCESS);
    }
  }
  catch(UnderFlowException e)
  {
   cout<<e.display();
  }
  catch(OverFlowException e)
  {
   cout<<e.display();
  }

 }// end of while(1)

       return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文