解決したいこと
c++で添付したソースコードをコンパイルすると以下のエラーメッセージが出て実行できません。
柴田望洋の「c++で学ぶオブジェクト指向プログラミング」を参考にしています。
調べて色々試したのですが、解決しません。何が原因なのでしょうか。
発生している問題・エラーメッセージ
MyException.cpp:12:23: error: looser throw specifier for ‘virtual const char* MyException::what() const’ 12 | virtual const char* what() const { return "マイ例外"; } | ^~~~ In file included from /usr/include/c++/9/system_error:41, from /usr/include/c++/9/bits/ios_base.h:46, from /usr/include/c++/9/ios:42, from /usr/include/c++/9/ostream:38, from /usr/include/c++/9/iostream:39, from MyException.cpp:1: /usr/include/c++/9/stdexcept:143:5: note: overridden function is ‘virtual const char* std::logic_error::what() const noexcept’ 143 | what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW; | ^~~~ MyException.cpp: In function ‘void work(int)’: MyException.cpp:19:46: error: no matching function for call to ‘std::exception::exception(const char [16])’ 19 | case 1: throw exception("exception例外"); | ^ In file included from /usr/include/c++/9/exception:38, from /usr/include/c++/9/ios:39, from /usr/include/c++/9/ostream:38, from /usr/include/c++/9/iostream:39, from MyException.cpp:1: /usr/include/c++/9/bits/exception.h:68:5: note: candidate: ‘constexpr std::exception::exception(std::exception&&)’ 68 | exception(exception&&) = default; | ^~~~~~~~~ /usr/include/c++/9/bits/exception.h:68:15: note: no known conversion for argument 1 from ‘const char [16]’ to ‘std::exception&&’ 68 | exception(exception&&) = default; | ^~~~~~~~~~~ /usr/include/c++/9/bits/exception.h:66:5: note: candidate: ‘constexpr std::exception::exception(const std::exception&)’ 66 | exception(const exception&) = default; | ^~~~~~~~~ /usr/include/c++/9/bits/exception.h:66:15: note: no known conversion for argument 1 from ‘const char [16]’ to ‘const std::exception&’ 66 | exception(const exception&) = default; | ^~~~~~~~~~~~~~~~ /usr/include/c++/9/bits/exception.h:63:5: note: candidate: ‘std::exception::exception()’ 63 | exception() _GLIBCXX_NOTHROW { } | ^~~~~~~~~ /usr/include/c++/9/bits/exception.h:63:5: note: candidate expects 0 arguments, 1 provided ---
該当のソースコード
c++
1#include <iostream> 2#include <exception> 3#include <stdexcept> 4 5using namespace std; 6 7class MyException : public logic_error 8{ 9public: 10 MyException() : logic_error("マイ例外") {} 11 12 virtual const char* what() const { return "マイ例外"; } 13}; 14 15void work(int sw) 16{ 17 switch(sw) 18 { 19 case 1: throw exception("exception例外"); 20 case 2: throw logic_error("logic_error例外"); 21 case 3: throw MyException(); 22 } 23} 24 25void test(int sw) 26{ 27 try 28 { 29 work(sw); 30 } 31 32 catch(const MyException& e) 33 { 34 cout << e.what() << "を補足。対処完了!!\n"; 35 } 36 37 catch(const logic_error& e) 38 { 39 cout << e.what() << "を補足。対処断念!!\n"; 40 throw; 41 } 42 43 catch(const exception& e) 44 { 45 cout << e.what() << "を補足。対処断念!!\n"; 46 throw "ABC"; 47 } 48} 49 50int main() 51{ 52 int sw; 53 54 cout << "sw: "; 55 cin >> sw; 56 57 try 58 { 59 test(sw); 60 } 61 62 catch(const logic_error& e) 63 { 64 cout << e.what() << "を補足。\n"; 65 } 66 67 catch(const char* e) 68 { 69 cout << e << "を補足。\n"; 70 } 71}
これかな?
https://stackoverflow.com/questions/35763027/looser-throw-specifier-for-virtual-const-char-ro-errstderrwhat-const
ご回答いただきありがとうございます。
回答を頂けたおかげで"what()"に関するエラーは解決することが出来ましたが、19行目の "case 1: throw exception("exception例外");"で出ている"no matching function for call to std::exception::exception(const char [16])"のエラーは解決できていません。どなたかわかる方,このエラーの解決方法を教えていただけないでしょうか。このエラーについてググってみてもあまりピンとこなくて。
> 無事、解決することが出来ました。
解決したのなら、SaitoAtsushiさんの回答をベストアンサーにしましょう
ベストアンサーにするのを忘れていました。すみません。ご指摘ありがとうございます。

回答2件
あなたの回答
tips
プレビュー