undefined reference to `vtable
admin
2024-01-31 23:27:46
0

c++ 编译错误

undefined reference to vtable
undefined reference to Abstract_base::~Abstract_base()

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\ssas0e\AppData\Local\Temp\ccwtXlSL.o:abstract_class.cpp:(.text$_ZN13Abstract_baseC2Ev[__ZN13Abstract_baseC2Ev]+0xa): undefined reference to `vtable for Abstract_base'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\ssas0e\AppData\Local\Temp\ccwtXlSL.o:abstract_class.cpp:(.text$_ZN17derived_from_baseC1Ev[__ZN17derived_from_baseC1Ev]+0x4d): undefined reference to `Abstract_base::~Abstract_base()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\ssas0e\AppData\Local\Temp\ccwtXlSL.o:abstract_class.cpp:(.text$_ZN17derived_from_baseD1Ev[__ZN17derived_from_baseD1Ev]+0x3e): undefined reference to `Abstract_base::~Abstract_base()'

错误代码示例子

#include using namespace std;class Abstract_base
{public:virtual ~Abstract_base();virtual void interface() const{};};class derived_from_base : public Abstract_base
{public:derived_from_base(){cout <<"derived ctor" << endl;}~derived_from_base() override{cout << "~ derived dtor" << endl;}void interface() const override{}};int main()
{derived_from_base b;return 1;
}

分析

vtable是虚函数表,报错说的是 undefined reference to
就是说,指向这个表的reference是未定义的。
为什么? 这个表不存在? 还是reference不存在?

子对象构造的过程

derived_from_base b 开始构造时候,先构造父类的,然后构造自己的,是一种自上而下的过程。我们看到编译的错误也是先父类出的问题,然后是子类出的问题。

父类哪里出的问题?
我们看下父类构造virtual fucntion过程。

  • 每一个class 产生出一堆指向virtual functions的指针,放在表格中。这个表格称为virtual table vtbl
  • 每一个class object被安插一个指针,指向相关的virtual table。通常这个指针被称为vptr. vptr的设定和重置都是由每一个class的constructor 和destructor和copy assignment运算符自动完成的。每个class所关联的type_infor_object(用以支持runtime type identification)也经由virtual table被指出来,通常放在表格的第一个slot.

国外一个针对此问题更加详细的讨论
总结下

  • look at your class definition. Find the first non-inline virtual function that is not pure virtual (not “= 0”) and whose definition you provide (not “= default”).

    If there is no such function, try modifying your class so there is one. (Error possibly resolved.)
    See also the answer by Philip Thomas for a caveat.

  • Find the definition for that function. If it is missing, add it! (Error possibly resolved.)

    If the function definition is outside the class definition, then make sure the function definition uses a qualified name, as in ClassName::function_name.

  • Check your link command. If it does not mention the object file with that function’s definition, fix that! (Error possibly resolved.)

  • Repeat steps 2 and 3 for each virtual function, then for each non-virtual function, until the error is resolved. If you’re still stuck, repeat for each static data member.

回到最初问题

上述代码的错误,是未定义function,只是声明。所以function pointer应该没产生成功,更别说后面的virtual table了。

相关内容

热门资讯

一天22个大V账号被封 平台出... 雪球大V“金浤”被罚之后,一天之内,22个大V账号被永久封禁,平台连续出手整治涉资本市场违规行为。 ...
罕见!自砍七成融资规模,百瑞吉... 导读:过去一年中,为了尽快向A股大门靠近,百瑞吉还付出了不小的代价——大幅削减融资规模。近年来,拟北...
氢能商业化难题待解,记者实探云... 本报(chinatimes.net.cn)记者胡雅文 曲靖报道 氢能商业化仍在探索中。 近年来国家和...
原创 西... 哥本哈根街头,上万名示威者举着“绝不出售格陵兰岛”的标语走向美国驻丹麦大使馆,寒冷的北欧空气中弥漫着...
成品油价或年内首涨! 今日(1月20日)24时,国内成品油新一轮调价窗口将开启。综合多家机构预测,成品油价或年内首涨,消费...
原创 电... 外资来了,不是随便逛街,是掏钱出手,高盛、瑞银、摩根士丹利、摩根大通四家都在名单上,其中三家在202...
信用卡分期纳入贴息,多家银行迅... 【大河财立方消息】1月20日,财政部、中国人民银行、金融监管总局发布《关于优化实施个人消费贷款财政贴...
原创 金... “好家伙,前几天还说金价破纪录了,这转头又掉下来了?这过山车坐的,心脏真受不了。”最近但凡关心点金价...
卖不动了?巨头宣布:降价!10... 自2022年见顶以来,受主要消费国奢侈品消费降温、培育钻石日益走俏等因素影响,钻石行业正遭遇现代史上...
鸣鸣很忙开启招股,2万家店撑起... 1月20日,中国最大的休闲食品饮料连锁零售商——湖南鸣鸣很忙商业连锁股份有限公司(下称“鸣鸣很忙”)...
原创 继... 去年九月的那个深夜,白俄罗斯边境毫无征兆地熄火了,原本跑得飞快的中欧班列,在那一刻被人按下了暂停键。...
供应扰动再发酵,碳酸锂尾盘涨停... 经历了一次急速回调后,碳酸锂期货又杀了一个“回马枪”。 1月20日,碳酸锂期货主力合约在尾盘触及涨停...
原创 融... 2026年伊始,A股市场便以一派火热景象迎接投资者,市场热度在多个维度均有所体现,尤其以融资余额的迅...
财经老王丨140万亿元 跟咱老... 来源:甘肃网络广播电视台 2025年,中国经济总量突破140万亿元。这个数字跟咱们老百姓有啥关系呢?...
小红书奶茶养号秘诀——4288... 大家好,我是4288养号盒子,提供专业免费养号软件,不仅有抖音养号,还有小红薯养号等、还有短视频热门...
原创 干... 美国这些年总想在经济上给中国使绊子,从贸易战打到科技限制,可中国那边经济还是稳稳的往前拱。2018年...
纪念币纪念钞二手交易莫冲动 来源:经济日报 目前,二手市场高价接盘的风险很高。一方面,卖家无实物,且二手市场缺乏权威验货渠道,买...
“十年十亿”魔咒告破?全球巨头... 2026年开年,AI制药正以前所未有的速度冲击传统研发的“双十定律”——一款新药研发需要十年时间、耗...
红杉资本考虑参投Anthrop... 红杉资本正准备加入Coatue和新加坡主权财富基金GIC,参与人工智能初创公司Anthropic P...
大行回应!消费贷贴息政策升级,... 来源:第一财经 消费贷贴息政策迎来重要升级,落地细节备受关注。1月20日晚间,多家国有大行表态将积极...