Undefined symbols "vtable for ..." and "typeinfo for..."?
Solution 1:
If Obstacle is an abstract base class, then make sure you declare all its virtual methods "pure virtual":
virtual void Method() = 0;
The = 0
tells the compiler that this method must be overridden by a derived class, and might not have its own implementation.
If the class contains any non-pure virtual functions, then the compiler will assume that they have an implementation somewhere, and its internal structures (vtable and typeinfo) might be generated in the same object file as one of those; if those functions are not implemented, then the internal structures will be missing and you will get these errors.
Solution 2:
The class Obstacle needs a virtual destructor. Change the destructor definition to be:
virtual ~Obstacle();
The definition of a destructor also creates the vtable for a class with virtual functions. It also ensures that a delete of a derived class instance through a base class pointer does the right thing.
(copy of my answer to question What should I do with this strange error? which seems to be a duplicate.)
Solution 3:
There is another reason you can get this error, and just want to document it here. I was linking with a static library which did not have RTTI. So Using the C++ flag -fno-rtti
fixed for me. If you do not need RTTI, you can using this flag as well. Hope this helps.
Solution 4:
Do you have an Obstacle.cc
file? If so, you need to make sure it gets built into Obstacle.o
, and that Obstacle.o
gets added to the command line when you link your program.
Also, make sure that you define all of the non-pure-virtual methods you declare. If you declare a pure virtual destructor, you need to define that too.
Solution 5:
vtable and typeinfo are internal structures generated by the C++ compiler. vtable is used for calling virtuable functions and typeinfo is used for RTTI.
Different compilers have different strategies for when they generate these structures. One strategy I've seen is they will generate the table in the same object file that contains the first virtual function in the class.