首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:“;”标记之前的预期主表达式

错误:“;”标记之前的预期主表达式
EN

Stack Overflow用户
提问于 2013-08-01 16:20:36
回答 5查看 7.2K关注 0票数 2

我知道错误了

代码语言:javascript
运行
复制
error: expected primary-expression before ';' token

当我试图编译以下代码时。有什么问题吗?

代码语言:javascript
运行
复制
#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;

class MyException : public std::invalid_argument{};

int main() {
    try {
        throw MyException; //here is the problem
    }
    catch (...){
    }

    return 0;
}

我也试过这段代码

代码语言:javascript
运行
复制
#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;

class MyException : public std::invalid_argument{};

int main() {
    try {
        throw MyException(); //here is the problem
    }
    catch (...){
    }

    return 0;
}

但我又犯了一个错误

代码语言:javascript
运行
复制
main.cpp: In constructor ‘MyException::MyException()’:
main.cpp:6:7: error: no matching function for call to ‘std::invalid_argument::invalid_argument()’
main.cpp:6:7: note: candidates are:
In file included from main.cpp:2:0:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:86:14: note: std::invalid_argument::invalid_argument(const string&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:86:14: note:   candidate expects 1 argument, 0 provided
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:83:9: note: std::invalid_argument::invalid_argument(const std::invalid_argument&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/stdexcept:83:9: note:   candidate expects 1 argument, 0 provided
main.cpp: In function ‘int main()’:
main.cpp:10:27: note: synthesized method ‘MyException::MyException()’ first required here 
EN

Stack Overflow用户

发布于 2013-08-01 16:41:58

这是你的主要方法

代码语言:javascript
运行
复制
int main() 
{  
   try {  
          throw MyException; //here is the problem  
   }  
   catch (...){      
   }  
   return 0;  
}    

解决方案:

代码语言:javascript
运行
复制
int main() 
{  
   try {
          MyException e;
          throw e; //problem solved    
   }  
   catch (...){      
   }  
   return 0;  
}  

说明:您看,MyException是一个类。您可以抛出类的一个对象。

票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17999422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档