Overloading STD :: Whow()无法转换const char*

overloading std::what() cannot convert const char*

本文关键字:转换 const char STD Whow Overloading      更新时间:2023-10-16

我正在写我的异常类:

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用G 编译,并在函数中获取返回行的以下错误():

无法转换'std :: __ cxx11 :: basic_string< _ chart,_traits,_alloc> :: c_str,std,std :: allocator>'type'const char*(std :: __ __ __ cxx11 :: basic_ basic__basic__string ::::: ::)const ::)'键入'const char*' 返回m_errormsg.c_str;

我做错了什么?谢谢

使用 return m_errorMsg.c_str();,它应该工作- c_str是a function ,而不是 actibal 。。