从std::exception继承的error类编译错误

C++ - Compilation error with Error class that inherit from std::exception

本文关键字:编译 error 错误 继承 std exception      更新时间:2023-10-16

这里是我的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;
};
相关文章: