我看过这个问题,看不出问题出在哪里.我不是C++的专家,所以对我来说这看起来不错.我上次尝试时用于编译没有问题.
namespace yaaf {
/************************************************************************/
/* */
/* Standard YAAF Errors */
/* */
/************************************************************************/
/* XGYAAFError
*
* YAAF Error; this is the root of my YAAF errors, and is
* a descendant of the standard exception class
*/
class XGYAAFError : public std::exception {
public:
explicit XGYAAFError(const char *);
explicit XGYAAFError(const std::string &err);
const char *what() const throw()
{
return fError.c_str();
}
private:
std::string fError;
};
} // namespace yaaf
#endif
Run Code Online (Sandbox Code Playgroud)
GCC库基类......
/**
* @brief Base class for all library exceptions.
*
* This is the base class for all exceptions thrown by the standard
* library, and by certain language expressions. You are free to derive
* your own %exception classes, or use a different hierarchy, or to
* throw non-class data (e.g., fundamental types).
*/
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char* what() const throw();
};
Run Code Online (Sandbox Code Playgroud)
当我尝试构建时,错误"覆盖函数的规范比基本版本更宽松"是我现在得到的.
我认为这可能与C++语言的变化(大约2004年??)有关,你可以在派生类中声明指针.但我不确定这是不是这样,以及如何解决这个问题.
任何有关具体错误或如何解决这个问题的想法都表示赞赏.
谢谢
XGYAAFError有一个类型的成员变量std::string,它有一个可以抛出任何异常的非平凡的析构函数. std::exception如你所见,有一个用户声明的析构函数,声明为不抛出异常.
因此,由于重写规则,XGYAAFError需要一个带有throw()异常规范的用户声明的析构函数.我在问题"异常规范如何影响虚拟析构函数覆盖?"中提供了对该主题的深入解释.有关详细信息,请参阅该问