0

Well, it's really strange what's happening to me, but I'll try to make it clear. I have a class and in one method I decide to put a throw (in hpp definition and in cpp implementation). so I have my method that can throw a std::exception. Here no problem.

I create an exception of mine:

class MyException : public std::exception {
public:
   MyException() throw();
   ~MyException() throw();
   const char what() const throw();
}

ok, let's use it in my methods from:

class myclass {
   void mymethod() throw(std::exception);
}

To:

class myclass {
   void mymethod() throw(MyException); // I have included hpp file where MyException is defined
}

OK! this is what I get

/tmp/ccwSS5GE.o:(.gcc_except_table+0x84): undefined reference to 'typeinfo for MyException' collect2: ld returned 1 exit status

WHY?? With std::exception everything works fine, now nothing works fine.

SoapBox
  • 20,457
  • 3
  • 51
  • 87
Andry
  • 16,172
  • 27
  • 138
  • 246
  • 2
    [This thread should help](http://stackoverflow.com/questions/307352/g-undefined-reference-to-typeinfo). Check out specifically [Tyler McHenry's answer](http://stackoverflow.com/questions/307352/g-undefined-reference-to-typeinfo/307440#307440) - if you still can not solve it, please show us all your code and how you try to use it. – wkl Nov 26 '10 at 00:08
  • 5
    The linker didn't crash. It simply reported an error in your program. – Alex Budovski Nov 26 '10 at 00:17
  • 1
    Linking order plays role in unix. try that? –  Nov 26 '10 at 00:21

1 Answers1

2

I think the OP code should give a compilation error as it is ill-formed and not in the zone of UB (which may explain linker error which is surprising here).

I guess the problem is your declaration.

const char what() const throw();

The return type 'const char' in your class is not covariant with the one in the base class std::exception which is defined as

virtual const char* std::exception::what()  const throw () 
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • Well, thank you for your suggestion. I am sorry I bad typed that char which is a char* !! Well, in the end I could solve it, but it is strange, I did not create a cpp file with implementations for those methods, maybe this caused the compiler to get really mad because when encountering the throw and looking for elements of that exception type, he did not find a good implementation and got crazy. Do you think it is a good explaination? – Andry Nov 26 '10 at 07:58