我已经派生了一个异常类std::runtime_error
,以便添加对异常流的支持。我收到一个奇怪的编译器错误输出,我不确定如何解决?
clang++ -std=c++11 -stdlib=libc++ -g -Wall -I../ -I/usr/local/include Main.cpp -c
Main.cpp:43:19: error: call to deleted constructor of 'EarthException'
throw EarthException(__FILE__, __LINE__)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../EarthException.hpp:9:12: note: function has been explicitly marked deleted here
struct EarthException : public Exception<EarthException>
template <typename TDerived>
class Exception : public std::runtime_error
{
public:
Exception() : std::runtime_error("") {}
Exception(const std::string& file, const unsigned line)
: std::runtime_error("")
{
stream_ << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
}
virtual ~Exception() {}
template <typename T>
TDerived& operator<<(const T& t)
{
stream_ << t;
return static_cast<TDerived&>(*this);
}
virtual const char* what() const throw()
{
return stream_.str().c_str();
}
private:
std::stringstream stream_;
};
struct EarthException : public Exception<EarthException>
{
EarthException() {}
EarthException(const std::string& file, const unsigned line)
: Exception<EarthException>(file, line) {}
virtual ~EarthException() {}
};
}
更新:
我现在添加了显式调用,std::runtime_error("")
因为它指出默认构造函数被标记为=delete
但是错误仍然存在。