从std::exception继承的error类编译错误
C++ - Compilation error with Error class that inherit from std::exception
这里是我的Error类来处理错误与try &问题:
#include <stdexcept>
#include <string>
class Error : public std::exception
{
public:
Error(const std::string&) throw();
~Error() throw();
const char* what() const throw();
private:
std::string _msg;
};
和cpp文件:
#include "Error.hpp"
Error::Error(const std::string& msg) throw()
: _msg(msg)
{
}
Error::~Error() throw()
{
}
const char* Error::what() const throw()
{
return (_msg.c_str());
}
和我有这个错误,而编译:
main.o:(.gcc_except_table+0x34): undefined reference to `typeinfo for Error'
MailBox.o: In function `MailBox::MailBox(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
MailBox.cpp:(.text+0x245): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x268): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x270): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x2f0): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x313): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x31b): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x3d6): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x3f9): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x401): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x452): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x475): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x47d): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x50a): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x52d): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x535): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x6af): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x6d2): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x6da): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x854): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x877): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x87f): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x923): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x946): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x94e): undefined reference to `typeinfo for Error'
MailBox.cpp:(.text+0x9b6): undefined reference to `Error::Error(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
MailBox.cpp:(.text+0x9d9): undefined reference to `Error::~Error()'
MailBox.cpp:(.text+0x9e1): undefined reference to `typeinfo for Error'
collect2: ld returned 1 exit status
我已经在另一个项目中使用了这个Error类,它工作得很好。我不明白为什么这里不工作
这是不是编译错误,这是链接器错误。基本上,这个错误告诉您,某些函数的定义丢失了。
从链接器的输出中可以明显看出,这些函数是类Error
的复制构造函数和析构函数。
这与您只显示这些函数的声明(在Error
的类定义中)的事实是兼容的。您还应该为它们提供一个定义。例如,您可以简单地内联这些定义:
class Error : public std::exception
{
public:
Error(const std::string& s) throw() : _msg(s) { }
~Error() throw() { };
const char* what() const throw() { return _msg.c_str(); };
private:
std::string _msg;
};
相关文章:
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 在 armhf 上使用 cmake 在 qt 上的编译问题 "Syntax error: wird unexpected (expecting " ) " "
- 当我编译XG-Bosst时,出现错误"error: 'Split' is not a member of 'xgboost::common' ……"
- 为什么GCC可以编译std::exception("some error msg")而没有错误?
- C++编译错误,"malloc: *** error for object: pointer being freed was not allocated"
- 编译 C++ 代码时"Error: comparison between pointer and integer"
- 编译 gsoap-onvif 求解 #error:M_ASN1_STRING_data“未在此范围内声明
- 编译 CPP 应用程序时出错。 "error: 'posix_memalign' was not declared in this scope"
- 在 Solaris 机器上使用 SUN C++ 进行编译时"Error: incomplete type "无符号字符 [] " "
- 我在 MinGW 的 Shell 中编译 libtcod 时遇到"Error 1"
- 在编辑器中"Error: type name is not allowed"消息,但不在编译期间
- 在 64 位 Windows 7 上使用 MinGW 进行编译时"Error: bad register name `%rax'"
- C++简单的类程序无法编译。我搞砸了包含标题吗? 'Error redefinition of a class'
- C++ Unordered_Map:在编译时接收"error: expected unqualified-id before ‘[’ token"
- 使用新的 Qt5 信号/插槽机制连接 QTcpSocket::error() 时编译错误
- 我在编译时收到此"error: 'string' not declared"
- 编译失败,出现"error: stray '342' in program"
- 从C++连接MySQL时程序错误中"error: stray ‘302’"编译错误
- 未定义对的引用.error(Linux)-在OSX中编译良好
- 正在为客户端/服务器编译项目[Linker error]未定义对的引用