以下のコードに問題があります。
#include <iostream>
#include <stdexcept>
class MyException : public std::logic_error {
};
void myFunction1() throw (MyException) {
throw MyException("fatal error");
};
void myFunction2() throw (std::logic_error) {
throw std::logic_error("fatal error");
};
int main() {
try {
myFunction1();
//myFunction2();
}catch (std::exception &e) {
std::cout << "Error.\n"
<< "Type: " << typeid(e).name() << "\n"
<< "Message: " << e.what() << std::endl;
}
return 0;
}
throw MyException("fatal error");
行が機能しません。Microsoft Visual Studio 2012 は次のように述べています。
error C2440: '<function-style-cast>' : cannot convert from 'const char [12]' to 'MyException'
MinGW の反応は非常に似ていました。
これは、コンストラクターが親クラスから子クラスにコピーさstd::logic_error(const string &what)
れなかったことを意味します。なんで?
ご回答有難うございます。