Pergunta

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.

Foi útil?

Solução

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 () 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top