编译报错 undefined reference to “vtable for child”

先说结果: 父类定义的虚函数,子类如果重载了 ,那就必须定义具体实现,不能弄个没有函数体的函数名放那里碰瓷。 

#include<iostream>

using namespace std;

class parent
{
public:
	parent(){
		std::cout<<"parent constructor no param"<<std::endl;
	};
private:
	parent(int a){
		std::cout<<"parent constructor: "<<a<<std::endl;
	};
public:
	virtual ~parent(){
		std::cout<<"parent disconstructor~"<<std::endl;
	};
	virtual void opt(){
		std::cout<<"parent opt"<<std::endl;
	};
};

class child:public parent
{
public:
	child(int a){
		cout<<"child constructor: "<<a<<endl;
	};
	~child();
	void opt(){
		std::cout<<"child opt"<<std::endl;
	};
	void child_do(){
		std::cout<<"child do"<<std::endl;
	};
};

int main()
{
	int b = 10;
	parent* test = new child(b);
	test->opt();
	reinterpret_cast<child*>(test)->child_do();
        delete parent;
	return 0;
}
/tmp/cc3sxtLw.o: In function `child::child(int)':
test.cpp:(.text._ZN5childC2Ei[_ZN5childC5Ei]+0x1d): undefined reference to `vtable for child'

找了好一会,发现是子类的析构函数只是声明了,没有定义,

改成 ~child(){}; 定义上就好了。

但是我记得之前也有过忘记定义析构函数具体内容的情况,编译并没有报错,

于是回过头注意到 这句报错的含义好像并不是说子类的析构函数没有定义,

undefined reference to `vtable for child' -> 对child的虚表未定义的引用,

这里 vtable 应该就是指的虚函数表,

尝试着换一种方式修改 ~child(); 这个不变, 把 virtual ~parent 改为 ~parent,

果然 也不报错了,但是我们都知道这样改的后果是啥,就是父类指针指向的子类对象析构不会调用子类的析构函数了。

[root@iZuf6iemoke3l0asocwqo5Z 继承]# ./a.out 
parent constructor no param
child constructor: 10
child opt
child do
parent disconstructor~
[root@iZuf6iemoke3l0asocwqo5Z 继承]#

所以,老老实实给父类的析构函数加上 virtual, 然后完善对子类析构函数的定义.

virtual ~parent(){
	std::cout<<"parent disconstructor~"<<std::endl;
};
~child(){
	cout<<"child disconstructor"<<endl;
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tobybo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值