tags:

views:

413

answers:

4

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  A: 

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.

R Samuel Klatchko
as far as the error above is concerned, what does undefined symbols mean ?
Lisa
+1  A: 

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.

bk1e
must i have an Obstacle.cc, since it has only some virtual functions?
Lisa
@Lisa: not necessarily, but defining non-inline non-template methods/functions in a .h file is likely to lead to a different linker error (multiple definitions). So I'll assume that everything in Obstacle is inline. Did you write definitions for all of Obstacle's member functions, including the constructor(s) and destructor? Note that if Obstacle has a pure virtual destructor, you are still required to write a definition for it.
bk1e
Does Obstacle have only pure virtual functions? Strike that--it doesn't matter when it comes to vtables. You need an Obstacle.o so the compiler has somewhere to store Obstacle's virtual table and type info.
outis
@outis: no, you don't need an object file. If all the virtual member functions are either inline or pure virtual, then the compiler must find somewhere to put the vtable. This typically done by generating a "link once" section in every object file.
Mike Seymour
+2  A: 

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.

Mike Seymour
I just came across this problem in my own code. I found this page through Google search, and I made the change you said and it fixed it. Thanks! :D
aip.cd.aish
A: 

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 http://stackoverflow.com/questions/1694785/what-should-i-do-with-this-strange-error which seems to be a duplicate.)

janm