问题
Nearly the final step but still some strange erros....
bash-3.2$ make
g++ -Wall -c -g Myworld.cc
g++ -Wall -g solvePlanningProblem.o Position.o AStarNode.o PRM.o PRMNode.o World.o SingleCircleWorld.o Myworld.o RECTANGLE.o CIRCLE.o -o solvePlanningProblem
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
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [solvePlanningProblem] Error 1
What's the meaning of vtable and typeinfo?
回答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.
回答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.)
回答3:
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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/1693634/undefined-symbols-vtable-for-and-typeinfo-for