【发布时间】:2019-11-30 02:32:32
【问题描述】:
我收到编译错误。我无法弄清楚根本原因是什么。
我试图评论基类的构造函数/析构函数。它奏效了。 但是取消注释会导致编译错误。
#include<iostream>
using namespace std;
class BaseException{
public:
BaseException();
virtual ~BaseException();
virtual void dumpmsg() = 0;
};
class DerivedException : public BaseException
{
public:
DerivedException():BaseException()
{
}
void dumpmsg() override
{
std::cout<< "Child class exception cuaght" << std::endl;
}
};
void h()
{
throw ( DerivedException() );
}
void g()
{
h();
}
void f()
{
g();
}
int main()
{
try{
f();
}
catch( BaseException &ex )
{
ex.dumpmsg();
}
}
错误信息:
In function `ZN16DerivedExceptionC1Ev`:
15 `undefined reference to BaseException::BaseException()`
In function `ZN16DerivedExceptionD1Ev`:
12 `undefined reference to BaseException::~BaseException()`
[Error] ld returned 1 exit status
【问题讨论】:
-
链接器说你还没有定义(实现)
BaseException构造函数和析构函数。您在哪里定义(实施)它们?我只能看到他们的声明。 -
它们是链接器错误,而不是编译错误。
BaseException的构造函数和析构函数都声明了但没有定义。