缺少类方法时出现奇怪的错误消息
可能的重复:
GCC C++ 链接器错误:未定义的引用“XXX 的 vtable”,对“ClassName::ClassName()”的未定义引用
由于一个奇怪的 ld 错误,我已经用头撞墙很长时间了。 所以我在一个小测试用例中重现了它来理解这个问题。
我声明了一个类,并在头文件中派生了另一个类:
class BaseClass {
public:
BaseClass(){};
virtual void func(){};
};
class DerivedClass: public BaseClass {
public:
DerivedClass();
void func();
};
然后我定义了构造函数,但忘记定义 func (这里是自愿的,但这实际上是我通过愚蠢的复制/粘贴所做的... ):
DerivedClass::DerivedClass(){
cout << "Derived constructor" << endl;
}
//void DerivedClass::func(){
// cout << "Derived func" << endl;
//}
然后我得到:
对“DerivedClass 的 vtable”的未定义引用
编辑:消息指向构造函数的声明!
如果我取消注释 func
的定义,那么我就没有错误。所以我的问题是: 为什么链接器没有告诉我 func
的定义丢失了?
当您有经验时,解决方案可能是显而易见的,但对于像我这样的初学者来说却不是!
感谢您的帮助。
Possible Duplicate:
GCC C++ Linker errors: Undefined reference to 'vtable for XXX', Undefined reference to 'ClassName::ClassName()'
I have been banging my head against a wall for a long time because of a strange ld error.
So I reproduced it in a small test case to understand the issue.
I declared a class and I derived another one in a header file:
class BaseClass {
public:
BaseClass(){};
virtual void func(){};
};
class DerivedClass: public BaseClass {
public:
DerivedClass();
void func();
};
Then I defined the constructor but forgot to define func
(voluntary here, but that actually what I did with a silly copy/paste...):
DerivedClass::DerivedClass(){
cout << "Derived constructor" << endl;
}
//void DerivedClass::func(){
// cout << "Derived func" << endl;
//}
Then I get:
undefined reference to `vtable for DerivedClass'
Edit: And the message points the declaration of the consctructor!
If I uncomment the definition of func
, then I have no error. So my question:
Why does the linker didn't tell me that the definition of func
is missing?
The solution might be obvious when you are experienced, but for a beginner like me it's not!
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
vtable
是为包含虚函数的类及其派生类创建的。这意味着在您的程序中vtable
将为BaseClass
创建和DerivedClass
。这些vtable
中的每一个都将包含虚拟函数void func()
的地址。现在请注意DerivedClass
> 不包含的定义void func()
,因此它的vtable
包含了BaseClass
的void func()
函数的地址。那就是为什么编译器给出错误未定义对 DerivedClass 的 vtable 的引用
。vtable
is created for the class that contains virtual function and for the classes derived from it.It means in your programvtable
will be created forBaseClass
andDerivedClass
.Each of thesevtables
would contain the address of virtual functionvoid func()
.Now note thatDerivedClass
doesn't contain the definition ofvoid func()
,hence itsvtable
contains the address ofBaseClass
'svoid func()
function.That's why the compiler is giving errorundefined reference to vtable for DerivedClass
.您的编译器似乎没有为
DerivedClass
生成vtable
。vtable
基本上是一个包含每个虚拟函数的函数指针的表。通过为每个对象存储指向类的vtable
(通常称为vptr
)的指针,可以在运行时确定要调用的正确虚拟函数。虽然它是特定编译器的实现细节,但这是实现虚函数的常用方法,虚函数在运行时绑定,因此不能像普通函数一样被调用。我的第一个猜测是编译器没有为
DerivedClass
生成vtable
,因为它没有定义任何虚拟函数,因此它使用BaseClass
> 的vtable
。虽然这有点奇怪,但这是我能想到的唯一想法。但我不太熟悉编译器架构,也许有人有更好的解决方案。It seems your compiler didn't generate a
vtable
for theDerivedClass
. Avtable
is basically a table that contains function pointers for every virtual function. By storing a pointer to the class'svtable
(usually calledvptr
) for every object the correct virtual function to call can be determined at runtime. Although it is an implementation detail of the specific compiler, this is the usual way to implement virtual functions, which are bound at runtime and can therefore not be called like normal functions.My first guess would be that the compiler didn't generate a
vtable
forDerivedClass
because it doesn't define any virtual functions so it uses theBaseClass
'svtable
. Although that's a bit strange, it is the only idea I can come up with. But I'm not that well-versed in compiler architecture and maybe somebody has a better solution.