缺少类方法时出现奇怪的错误消息

发布于 2024-12-06 04:10:12 字数 1063 浏览 0 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-12-13 04:10:12

vtable 是为包含虚函数的类及其派生类创建的。这意味着在您的程序中 vtable 将为 BaseClass 创建和 DerivedClass。这些 vtable 中的每一个都将包含虚拟函数 void func() 的地址。现在请注意 DerivedClass > 不包含的定义void func(),因此它的vtable包含了BaseClassvoid func()函数的地址。那就是为什么编译器给出错误未定义对 DerivedClass 的 vtable 的引用

vtable is created for the class that contains virtual function and for the classes derived from it.It means in your program vtable will be created for BaseClass and DerivedClass.Each of these vtables would contain the address of virtual function void func().Now note that DerivedClass doesn't contain the definition of void func(),hence its vtable contains the address of BaseClass's void func() function.That's why the compiler is giving error undefined reference to vtable for DerivedClass.

八巷 2024-12-13 04:10:12

您的编译器似乎没有为 DerivedClass 生成 vtablevtable 基本上是一个包含每个虚拟函数的函数指针的表。通过为每个对象存储指向类的vtable(通常称为vptr)的指针,可以在运行时确定要调用的正确虚拟函数。虽然它是特定编译器的实现细节,但这是实现虚函数的常用方法,虚函数在运行时绑定,因此不能像普通函数一样被调用。

我的第一个猜测是编译器没有为 DerivedClass 生成 vtable,因为它没有定义任何虚拟函数,因此它使用 BaseClass > 的vtable。虽然这有点奇怪,但这是我能想到的唯一想法。但我不太熟悉编译器架构,也许有人有更好的解决方案。

It seems your compiler didn't generate a vtable for the DerivedClass. A vtable is basically a table that contains function pointers for every virtual function. By storing a pointer to the class's vtable (usually called vptr) 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 for DerivedClass because it doesn't define any virtual functions so it uses the BaseClass's vtable. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文