C++

重载虚函数一定要定义函数体

2011/2/25

重载虚函数一定要定义函数体时,否则会提示:

error LNK2001: unresolved external symbol "public: virtual void __thiscall

实例错误代码:

#include <iostream>
#define LOG(level) std::cout
using namespace std;
class A
{
public:
	virtual	void function1();
	 void go();
};
void A::go()
{
	cout<<"Can this class run success"<<endl;
}

class B:public A
{
public:
	void function2();
	void function1();
};

void B::function2()
{
	cout<<"is the problem of herit"<<endl;
}

int main()
{
	A a;
	a.go();

	B b;
	b.function2();
	return 0;
}

原因参考 《深度探索 —— C++内存对象分析》中 的runtime 和 virtual function的说明。

如果有机会,我会贴上我对这个问题的具体详解。