[英]overloading std::exception - what() cannot convert const char*
I am writing my exception class: 我正在编写异常类:
class MyExcept: public std::exception
{
public:
MyExcept(std::string _msg);
virtual ~MyExcept() throw();
virtual const char* what() const throw();
private:
std::string m_errorMsg;
};
MyExcept::MyExcept(std::string _msg)
: m_errorMsg(_msg)
{
}
MyExcept::~MyExcept() throw()
{
}
const char* MyExcept:: what() const throw()
{
return m_errorMsg.c_str;
}
I compiling with g++ and get the following error for the return line in the function what(): 我使用g ++进行编译,并在what()函数的返回行中收到以下错误:
cannot convert 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::c_str, std::allocator >' from type 'const char* (std::__cxx11::basic_string::)() const' to type 'const char*' return m_errorMsg.c_str; 无法将类型'const char *(std :: __ cxx11 :: basic_string ::)()const'转换为'std :: __ cxx11 :: basic_string <_CharT,_Traits,_Alloc> :: c_str,std :: allocator>' 'const char *'return m_errorMsg.c_str;
What I am I doing wrong? 我做错了什么? Thanks 谢谢
Use return m_errorMsg.c_str();
使用return m_errorMsg.c_str();
, and it should work - c_str
is a function , not a variable . ,并且应该可以工作c_str
是一个函数 ,而不是变量 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.