0

好吧,发生在我身上的事情真的很奇怪,但我会尽量说清楚。我有一个类,在一种方法中我决定抛出一个(在 hpp 定义和 cpp 实现中)。所以我有可以抛出 std::exception 的方法。这里没有问题。

我创建了一个例外:

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

好的,让我们在我的方法中使用它:

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

到:

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

好的!这就是我得到的

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

为什么??使用 std::exception 一切正常,现在一切正常。

4

1 回答 1

2

我认为 OP 代码应该给出编译错误,因为它格式错误并且不在 UB 区域中(这可能解释了链接器错误,这在这里令人惊讶)。

我想问题是你的声明。

const char what() const throw();

您的类中的返回类型“const char”与基类 std::exception 中定义为的返回类型不协变

virtual const char* std::exception::what()  const throw () 
于 2010-11-26T02:38:37.640 回答