我该怎么办这个奇怪的错误? [英] What should I do with this strange error?
问题描述
一切都很好,最后的问题是这么烦人。编译是伟大的但链接失败:
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
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.
推荐答案
你声明一个非抽象类 Obstacle
You declare a non-abstract class Obstacle
, but you don't implement all its member functions.
更好地声明为抽象类:
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(){}
};
原因是在许多C ++编译器中可以找到的启发式算法 - 避免不必要的创建重复
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.
您的代码掩盖了这个方案:你包括了一个类, Obstacle.hh
到一些编译单元,编译器看到一个类 Obstacle
,它具有 collidesWith
作为第一个非内联虚拟成员函数,但是它没有在当前编译单元中定义,因此编译器认为它可以延迟类的vtable和typeinfo的创建。因为没有 collidesWith
的定义,当程序被链接时,它们都会丢失。
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.
这篇关于我该怎么办这个奇怪的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!