我需要继承的异常类runtime_class来接受wstring&。这是MyExceptions.h:
using namespace std;
class myExceptions : public runtime_error
{
public:
myExceptions(const string &msg ) : runtime_error(msg){};
~myExceptions() throw(){};
};
Run Code Online (Sandbox Code Playgroud)
我愿意这样myExceptions接受: 。但是当我运行它时,我收到了这个错误:wstring&myExceptions(const **wstring** &msg )
C2664: 'std::runtime_error(const std__string &): cannot convert parameter 1 from 'const std::wstring' to 'const std::string &'
Run Code Online (Sandbox Code Playgroud)
我了解runtime_error接受string&而不是wstring&按照C++ 参考 - runtime_error中的定义:
> explicit runtime_error (const string& what_arg);
Run Code Online (Sandbox Code Playgroud)
我如何使用wstring&with runtime_error?
最简单的方法是传递给runtime_error常规消息并直接在myExceptions类中处理 wstring 消息:
class myExceptions : public runtime_error {
public:
myExceptions(const wstring &msg ) : runtime_error("Error!"), message(msg) {};
~myExceptions() throw(){};
wstring get_message() { return message; }
private:
wstring message;
};
Run Code Online (Sandbox Code Playgroud)
否则,您可以编写一个从 wstring 转换为 string 的私有静态成员函数,并使用它将转换后的字符串传递给 的runtime_error构造函数。然而,正如您从这个答案中看到的,这并不是一件非常简单的事情,对于异常构造函数来说可能有点太多了。