2014-03-13 73 views
2

我需要我的異常類,繼承runtime_class,接受wstring&。這是MyExceptions.hstring&vs. wstring&in runtime_error

using namespace std; 

class myExceptions : public runtime_error 
{ 

public: 
    myExceptions(const string &msg) : runtime_error(msg){}; 
    ~myExceptions() throw(){}; 

}; 

我想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 &' 

我明白runtime_error接受string&,而不是wstring&定義爲從C++ Reference - runtime_error如下:

> explicit runtime_error (const string& what_arg); 

我怎樣才能在runtime_error使用wstring&用?

+0

如何編寫以wstring爲參數的另一個構造函數? – taocp

+1

小的澄清:這是一個編譯錯誤,它恰好在錯誤消息中有運行時字樣。 –

+0

我是這麼想的,但我不知道該怎麼做。我應該用這種方式覆蓋它?顯式的runtime_error(const wstring & msg);和在MyExceptions.h類的地方? – Colet

回答

3

做最簡單的事情將是直接傳遞給runtime_error常規的消息,並處理您的wstring的消息在myExceptions類:

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; 
}; 

否則,你可以寫,從wstring的到轉換私有靜態成員函數字符串並使用它將轉換後的字符串傳遞給runtime_error的構造函數。但是,從this answer可以看出,這不是一件很簡單的事情,對於異常構造函數來說可能有點太多了。

+0

它的工作!謝謝@尼科拉! – Colet

相關問題