C++中的内联throw()方法

Grz*_*nio 6 c++ gcc throw

我试图定义一个非常简单的异常类.因为它很简单,我只想把它保存在.h文件中,但是编译器并不喜欢throw().代码:

#include <exception>
#include <string>

class PricingException : public virtual std::exception
{
private:
    std::string msg;
public:
        PricingException(std::string message) : msg(message) {}
        const char* what() const throw() { return msg.c_str(); }
        ~PricingException() throw() {}
};
Run Code Online (Sandbox Code Playgroud)

GCC给出以下错误:

/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:13: error: expected unqualified-id before ‘{’ token
/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:14: error: expected unqualified-id before ‘{’ token
Run Code Online (Sandbox Code Playgroud)

用于线throw().知道怎么解决吗?

编辑

我试图删除有问题的方法的主体,即

virtual ~PricingException() throw();// {}
Run Code Online (Sandbox Code Playgroud)

现在我得到更奇怪的错误信息:

/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:14: error: looser throw specifier for ‘virtual PricingException::~PricingException()’
/usr/include/c++/4.5/exception:65: error:   overriding ‘virtual std::exception::~exception() throw ()’
Run Code Online (Sandbox Code Playgroud)

它只是忽略了我的throw说明符!

Grz*_*nio 0

终于找到了!@Mike Seymour 的评论是正确的 - 事实证明,在文件 nr3.h (数字接收代码的一部分)中定义了一个宏throw(message)

我不明白的是为什么它会影响不包含此 .h 文件的文件的编译...

无论如何,我认为 Visual Studio 有不同的编译顺序或其他东西,所以它在那里编译而不是在 gcc 下编译纯属运气。