14

对于给定的类构造函数和析构函数,我遇到了一个与缺少 vtable 有关的非常奇怪的错误。请帮我解决这个问题。

架构 i386 的未定义符号:

  "vtable for A", referenced from:
      A::A() in A.o
      A::~MissionController() in A.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

代码片段;

.h 文件:

class A: public B

  public:
     A();
    ~A();

};

.cpp 文件..

 A::A()   
{


}

A::~A()
{


}
4

3 回答 3

8

找到它,尝试使用示例,这是一个示例。

class Shape{

public:
virtual int areas();
virtual void display();

virtual ~Shape(){};
};

编译器抱怨

Undefined symbols for architecture x86_64:
"typeinfo for Shape", referenced from:
  typeinfo for trian in main_file.o
 "vtable for Shape", referenced from:
  Shape::Shape() in main_file.o
  NOTE: a missing vtable usually means the first non-inline virtual member      function has no definition.
   ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  make: *** [cpp_tries] Error 1enter code here

修改为空或虚函数旁边 {} 内的任何内联内容

class Shape{

public:
    virtual int areas(){};
    virtual void display(){};

    virtual ~Shape(){};
};

基本上,它没有找到非内联虚函数的函数定义。

于 2016-02-03T02:32:00.500 回答
7

啊! 考虑到这一点,我想我明白了正在发生的事情。我打赌那CCNode是属于其他人的代码。

您继承的任何虚函数在派生类中也是虚函数......并且将析构函数设为虚拟是常见的做法......您可能没有意识到析构函数是虚拟的。

此外,如果您正在使用其他人的头文件,但忘记链接到他们的目标文件,则可能会导致此错误,因为链接器会缺少CCNode.

于 2013-03-07T10:31:53.180 回答
1

尝试将虚拟析构函数添加到您的类中。CCNode 可能包含一些虚拟方法,您的编译器无法处理它。

    class MissionController: public CCNode
    {

      public:
         MissionController();
        virtual ~MissionController();
    };

它是一些公共框架,我们在哪里可以看到 CCNode 类定义?请参阅vtable 以获取从编译错误 xcode 引用的 ..http://www.parashift.com/c++-faq-lite/link-errs-missing-vtable.html以获得更多帮助。

于 2013-03-07T10:18:45.590 回答