【问题标题】:c++ a missing vtable errorc++ 缺少 vtable 错误
【发布时间】:2013-02-22 07:00:48
【问题描述】:

我收到一个非常奇怪的错误,与给定类构造函数和析构函数缺少 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)

代码sn-p;

.h 文件:

class A: public B

  public:
     A();
    ~A();

};

.cpp 文件..

 A::A()   
{


}

A::~A()
{


}

【问题讨论】:

  • 注意:缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义。
  • 我认为您的错误出在您未发布的代码中。例如CCNode中定义了哪些virtual函数
  • 正如编译器所说,可能缺少虚函数。我经常忘记包含纯虚析构函数的定义,这在纯虚析构函数的情况下实际上是必需的。
  • @user1908860,如果您删除 CCNode 基类,则不可能出现此错误,因为如果类没有虚函数,则它不需要 vtable。所以你没有显示真实的代码,或者你在更改代码后没有正确(重新)构建你的项目,或者你在撒谎。
  • 我在尝试覆盖非虚拟父方法时遇到了类似的错误。

标签: c++


【解决方案1】:

找到了,试了下样例,这里有一个例子。

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(){};
};

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

【讨论】:

    【解决方案2】:

    啊!考虑到这一点,我想我明白发生了什么。我敢打赌CCNode 是属于其他人的代码。

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

    另外,如果您使用的是其他人的头文件,但忘记链接到他们的目标文件,则可能会导致此错误,因为链接器会缺少 CCNode 的析构函数。

    【讨论】:

      【解决方案3】:

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

          class MissionController: public CCNode
          {
      
            public:
               MissionController();
              virtual ~MissionController();
          };
      

      它是一些公共框架,我们在哪里可以看到 CCNode 类定义?如需更多帮助,请参阅 vtable for .. referenced from compile error xcodehttp://www.parashift.com/c++-faq-lite/link-errs-missing-vtable.html

      【讨论】:

        猜你喜欢
        • 2014-10-12
        • 2015-01-18
        • 2013-06-30
        • 2012-08-11
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多