«Если рабочий хочет хорошо выполнять свою работу, он должен сначала заточить свои инструменты» — Конфуций, «Аналитики Конфуция. Лу Лингун»
титульная страница > программирование > Why Am I Getting \"vtable\" and \"typeinfo\" Undefined Symbol Errors in My C++ Code?

Why Am I Getting \"vtable\" and \"typeinfo\" Undefined Symbol Errors in My C++ Code?

Опубликовано 8 ноября 2024 г.
Просматривать:399

Why Am I Getting \

Undefined Symbols: "vtable" and "typeinfo"

In the provided code, a linking error arises with the following messages:

Undefined symbols:
  "vtable for Obstacle", referenced from:
      Obstacle::Obstacle()in Myworld.o
  "typeinfo for Obstacle", referenced from:
      typeinfo for RECTANGLEin RECTANGLE.o
      typeinfo for CIRCLEin CIRCLE.o

Understanding "vtable" and "typeinfo"

  • vtable (Virtual Method Table): A data structure used by compilers to store pointers to the virtual methods of a class. It allows objects of derived classes to call the correct implementations of virtual methods.
  • typeinfo (Runtime Type Information): Information stored in the object's memory that identifies the type of the object. It enables dynamic type checking and polymorphic behavior.

Resolving the Error

The root cause of this error is most likely that the abstract base class Obstacle contains non-pure virtual functions. When a class declares a pure virtual function (virtual void Method() = 0;), it signifies that the derived classes must override the implementation.

If you have defined non-pure virtual functions in Obstacle, the compiler expects to find their implementations either in the base class or in the derived classes. However, if these implementations are not provided, the compiler generates internal structures (vtable and typeinfo) that rely on those functions. When linking, the missing functions lead to the undefined symbol errors.

Solution

To resolve this issue, ensure that all virtual methods in the abstract base class Obstacle are declared as pure virtual functions. This forces the derived classes to override and implement these methods. By doing so, the correct virtual method implementations will be provided, and the linking process should succeed without the undefined symbol errors.

Последний учебник Более>

Изучайте китайский

Отказ от ответственности: Все предоставленные ресурсы частично взяты из Интернета. В случае нарушения ваших авторских прав или других прав и интересов, пожалуйста, объясните подробные причины и предоставьте доказательства авторских прав или прав и интересов, а затем отправьте их по электронной почте: [email protected]. Мы сделаем это за вас как можно скорее.

Copyright© 2022 湘ICP备2022001581号-3