0

Here is my Error class to handle errors with try & catch :

#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;
    };

And the cpp file :

#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());
}

And I have this errors while compiling:

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

I've already used this Error class for another project, and it worked well. I don't understand why here it doesn't work.

0

1 Answer 1

3

This is not a compilation error, this is a linker error. Basically, this error is informing you that a definition for some your functions is missing.

Reading from the linker's output, it is evident that those functions are the copy constructor and the destructor of class Error.

This is compatible with the fact that you are showing just a declaration of those function (in the class definition for Error). You should also provide a definition for them. For instance, you could simply inline those definitions:

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;
};
Sign up to request clarification or add additional context in comments.

3 Comments

@Elfayer: Then the Error.cpp file does not get processed by the compiler somehow. Check your project settings and make sure the file is not excluded from the build. Also, to verify if this is the issue, try inlining those function definitions as I suggested. If it works, then the problem is definitely that Error.cpp does not get processed.
Oh my god... I've forget to put Error.cpp on my Makefile. I feel sooo stupid ! >< Let's say I'm tired...
@Elfayer: It just happens, no worries ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.