"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Published on 2024-11-08
Browse:367

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3