Undefined Symbols: "vtable" and "typeinfo"
Question:
When compiling a C program, the following errors occur:
Undefined symbols: "vtable for Obstacle" "typeinfo for Obstacle"
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:
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>
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!