未定义的符号“vtable for ...”和“...的类型信息”?

发布于 2024-08-10 04:27:17 字数 618 浏览 2 评论 0原文

快到最后一步了,但仍然出现一些奇怪的错误......

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

vtable 和 typeinfo 是什么意思?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无尽的现实 2024-08-17 04:27:18

您有 Obstacle.cc 文件吗?如果是这样,您需要确保将其内置到 Obstacle.o 中,并且在链接程序时将 Obstacle.o 添加到命令行中。

另外,请确保定义了声明的所有非纯虚拟方法。如果您声明一个纯虚拟析构函数,您也需要定义它。

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.

季末如歌 2024-08-17 04:27:18

vtable 和 typeinfo 是 C++ 编译器生成的内部结构。 vtable用于调用虚拟函数,typeinfo用于RTTI。

不同的编译器在生成这些结构时有不同的策略。我见过的一个策略是他们将在包含类中第一个虚拟函数的同一目标文件中生成表。

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.

梨涡少年 2024-08-17 04:27:18

如果您在子类中错误地声明了该方法但忘记实现它,则可能会出现此错误。

如果像我一样,如果您在中间类中实现该方法,甚至可能会发生这种情况。例如,

class Base {
public:
    virtual void func() = 0;
};

class Intermediate : public Base {
public:
    void func(); // implemented in source file
};

class Foo : public Intermediate {
    void func(); // not implemented and so a linker error will occur
};

You can get this error if you mistakenly declare the method in your subclass but forget to implement it.

This can even happen if, as happened to me, you implement the method in an intermediate class. For example,

class Base {
public:
    virtual void func() = 0;
};

class Intermediate : public Base {
public:
    void func(); // implemented in source file
};

class Foo : public Intermediate {
    void func(); // not implemented and so a linker error will occur
};
清秋悲枫 2024-08-17 04:27:17

如果 Obstacle 是抽象基类,那么请确保将其所有虚拟方法声明为“纯虚拟”:

virtual void Method() = 0;

= 0 告诉编译器该方法必须由派生类重写,并且可能没有它自己的实现。

如果类包含任何非纯虚函数,那么编译器将假设它们在某处有一个实现,并且其内部结构(vtable 和 typeinfo)可能会在与其中之一相同的目标文件中生成;如果这些功能没有实现,那么内部结构就会丢失,你会得到这些错误。

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.

許願樹丅啲祈禱 2024-08-17 04:27:17

Obstacle 类需要一个虚拟析构函数。将析构函数定义更改为:

virtual ~Obstacle();

析构函数的定义还为具有虚函数的类创建 vtable。它还确保通过基类指针删除派生类实例是正确的。

(我对问题的回答的副本 我应该如何处理这个奇怪的错误? 这似乎是重复的。)

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.)

只为守护你 2024-08-17 04:27:17

还有另一个原因会导致您出现此错误,并且只想在此处记录它。我正在链接一个没有 RTTI 的静态库。所以使用 C++ 标志 -fno-rtti 为我修复了。如果不需要 RTTI,也可以使用此标志。希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文