When you encounter the error message “undefined reference to typeinfo for base class,” it usually implies that the linker is unable to find the necessary type information for your base class. This error commonly occurs when you are attempting to use virtual functions or inheritance in C++.
One possible reason for this error is that you have declared a virtual function in your base class but failed to provide a definition for it. In this case, the linker is unable to resolve the function call from derived classes, leading to the “undefined reference to typeinfo for base class” error.
To better understand this issue, consider the following example:
<!-- File: BaseClass.h -->
class BaseClass {
public:
virtual void someFunction();
};
<!-- File: DerivedClass.cpp -->
#include "BaseClass.h"
class DerivedClass : public BaseClass {
public:
void someFunction() override;
};
<!-- File: main.cpp -->
#include "BaseClass.h"
int main() {
BaseClass* base = new DerivedClass();
base->someFunction();
delete base;
return 0;
}
In this example, we have a base class BaseClass
with a virtual function someFunction()
. The derived class DerivedClass
provides an implementation for this function. However, if we forget to define the function in the derived class like this:
<!-- Incorrect File: DerivedClass.cpp -->
#include "BaseClass.h"
class DerivedClass : public BaseClass {
// missing implementation for someFunction()
};
When you try to compile and link the above code, you may encounter the “undefined reference to typeinfo for BaseClass” error because the linker cannot find the required type information for the base class function. To resolve this error, make sure to provide a definition for all virtual functions declared in the base class.
Similar post
- Could not initialize class org.apache.ignite.ignitejdbcthindriver
- Could not initialize class com.android.sdklib.repository.androidsdkhandler
- Module ‘connectivity_plus’ not found
- Addition/subtraction of integers and integer-arrays with timestamp is no longer supported. instead of adding/subtracting `n`, use `n * obj.freq`
- Undefined constant “ldaprecord\models\attributes\ldap_escape_filter”