gpt4 book ai didi

c++ - test.cpp :(. text._ZN1AC2Ev[_ZN1AC5Ev]+0x8)||对 `vtable for A' 的 undefined reference |

转载 作者:太空宇宙 更新时间:2023-11-04 15:47:08 24 4
gpt4 key购买 nike

我有一个非常简单的代码:

#include <iostream>
#include <string>

class A
{
public :

A(){}
virtual void fun(std::string);
virtual ~A(){}
};

class B : public A
{
public :

B(){}
void fun(std::string);
~B(){};
};

void B::fun(std::string)
{

}

int main()
{
A a;
return 0;
}

我在尝试编译它时收到了这条消息:

/home/Temp/test.o||In function A::A()':|
test.cpp:(.text._ZN1AC2Ev[_ZN1AC5Ev]+0x8)||undefined reference to
vtable for A'| /home/Temp/test.o||In function A::~A()':|
test.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0xb)||undefined reference to
vtable for A'| /home/Temp/test.o:(.rodata._ZTI1B[typeinfo for B]+0x8)||undefined reference to `typeinfo for A'| ||=== Build finished: 3 errors, 0 warnings ===|

这是为什么?

最佳答案

您忘记实现 A::fun

如果你不想实现一个虚函数,用= 0把它标记为pure,但是注意这个类会变成抽象的,你将无法实例化它。

关于c++ - test.cpp :(. text._ZN1AC2Ev[_ZN1AC5Ev]+0x8)||对 `vtable for A' 的 undefined reference |,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14773958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com