Home > Backend Development > C++ > Why Am I Getting \'Undefined Symbols: \'vtable\' and \'typeinfo\'\' Errors in My C Program?

Why Am I Getting \'Undefined Symbols: \'vtable\' and \'typeinfo\'\' Errors in My C Program?

Linda Hamilton
Release: 2024-10-31 16:02:56
Original
420 people have browsed it

Why Am I Getting

Undefined Symbols: "vtable" and "typeinfo"

Question:

When compiling a C program, the following errors occur:

Undefined symbols:
  "vtable for Obstacle"
  "typeinfo for Obstacle"
Copy after login

What do these symbols mean, and how can they be resolved?

Answer:

In C , a virtual method is a method that is declared in a base class and overridden in derived classes. To allow dynamic binding to the correct method at runtime, the compiler generates a virtual method table (vtable) and type information (typeinfo) for each class that contains virtual methods.

The errors indicate that the compiler is unable to find the vtable and typeinfo for the Obstacle class. This can occur if:

  • The Obstacle class is not properly declared as abstract, or
  • The Obstacle class contains non-pure virtual methods that are not implemented in a derived class.

To resolve these errors, ensure that all virtual methods in the Obstacle class are declared as pure virtual, using the following syntax:

<code class="cpp">virtual void Method() = 0;</code>
Copy after login

This indicates that the method must be overridden in a derived class and that it may not have its own implementation in the base class.

If the Obstacle class contains any non-pure virtual methods, ensure that they are implemented in a derived class. Otherwise, the compiler will assume that they have an implementation somewhere and generate the vtable and typeinfo in an object file that does not contain the actual implementation.

The above is the detailed content of Why Am I Getting \'Undefined Symbols: \'vtable\' and \'typeinfo\'\' Errors in My C Program?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template