Everything is fine and the final problem is so annoying. Compile is great but link fails:
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**
Obstacle.hh
#ifndef Obstacle_hh
#define Obstacle_hh
#include <vector>
#include <iostream>
class Obstacle{
public:
Obstacle(){}
virtual bool collidesWith(double x,double y);
virtual void writeMatlabDisplayCode(std::ostream &fs);
virtual ~Obstacle(){}
};
#endif
What's the problem I have ? I can post any code you need to analyze it.
You declare a non-abstract class Obstacle, but you don't implement all its member functions.
Better declare it as abstract class:
class Obstacle{
public:
Obstacle(){} // this is superfluous, you can (and should) remove it
virtual bool collidesWith(double x,double y) = 0;
virtual void writeMatlabDisplayCode(std::ostream &fs) = 0;
virtual ~Obstacle(){}
};
The reason is a heuristic you'll find in many C++ compilers - to avoid the needless creation of duplicate vtables and typeinfos for a class they are created when its first non-inline virtual member function (if it has one) is defined.
Your code foils this scheme: You include Obstacle.hh into some compilation unit, the compiler sees a class Obstacle that has collidesWith as first non-inline virtual member function, but it isn't defined in the current compilation unit, so the compiler thinks it can defer the creation of vtable and typeinfo for the class. Because there is no definition of collidesWith, they both end up missing when the program is linked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With