为多态基类声明virtual析构函数的有关问题,无法通过编译
为多态基类声明virtual析构函数的问题,无法通过编译
effective c++ 第七条:为多态基类声明virtual析构函数
但是下面的代码编译不过,
#include <iostream>
using std::cout;
using std::endl;
class B
{
private:
int a;
public:
B(){};
virtual ~B() = 0;
};
class D:public B
{
public:
~D(){};
};
int main(int argc, char** argv)
{
B* pD = new D;
if(pD)
{
delete pD;
}
return 0;
}
编译信息:
------ Build started: Project: hello, Configuration: Debug Win32 ------
Compiling...
main.cpp
Linking...
main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall B::~B(void) " (??1B@@UAE@XZ) referenced in function "public: virtual __thiscall D::~D(void) " (??1D@@UAE@XZ)
Debug/hello.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://d:\vc projects\test\hello\Debug\BuildLog.htm "
hello - 2 error(s), 0 warning(s)
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped
请高手解答,谢谢!!
------解决方案--------------------
不要设为纯虚函数
------解决方案--------------------
实现~B();
------解决方案--------------------
因为你A里面有数据。
如果是纯虚析构函数,也要给出实现。
------解决方案--------------------
析构函数只要声明了就要实现,哪怕是纯虚的。
因为声明后系统就不会提供一个默认的隐式析构函数体了。而析构函数是每个类必须要有的。
effective c++ 第七条:为多态基类声明virtual析构函数
但是下面的代码编译不过,
#include <iostream>
using std::cout;
using std::endl;
class B
{
private:
int a;
public:
B(){};
virtual ~B() = 0;
};
class D:public B
{
public:
~D(){};
};
int main(int argc, char** argv)
{
B* pD = new D;
if(pD)
{
delete pD;
}
return 0;
}
编译信息:
------ Build started: Project: hello, Configuration: Debug Win32 ------
Compiling...
main.cpp
Linking...
main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall B::~B(void) " (??1B@@UAE@XZ) referenced in function "public: virtual __thiscall D::~D(void) " (??1D@@UAE@XZ)
Debug/hello.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://d:\vc projects\test\hello\Debug\BuildLog.htm "
hello - 2 error(s), 0 warning(s)
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped
请高手解答,谢谢!!
------解决方案--------------------
不要设为纯虚函数
------解决方案--------------------
实现~B();
------解决方案--------------------
因为你A里面有数据。
如果是纯虚析构函数,也要给出实现。
------解决方案--------------------
析构函数只要声明了就要实现,哪怕是纯虚的。
因为声明后系统就不会提供一个默认的隐式析构函数体了。而析构函数是每个类必须要有的。