C++ vtable for .. 从编译错误 xcode 引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1458180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vtable for .. referenced from compile error xcode
提问by Andres
I was getting the following error compiling an iPhone project:
我在编译 iPhone 项目时遇到以下错误:
"vtable for oned::MultiFormatUPCEANReader", referenced from:
__ZTVN4oned23MultiFormatUPCEANReaderE$non_lazy_ptr in MultiFormatUPCEANReader.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Anybody know how I may fix it?
有人知道我该如何解决吗?
回答by Andres
The problem seemed to be that in the class MultiFormatUPCEANReader I had declared a constructor and destructor, but had not written a body for the destructor, this was causing this annoying problem. Hope this helps somebody solve their compile error. This is a terrible compiler error with little information!
问题似乎是在 MultiFormatUPCEANReader 类中我声明了一个构造函数和析构函数,但没有为析构函数编写主体,这导致了这个烦人的问题。希望这有助于有人解决他们的编译错误。这是一个可怕的编译器错误,信息很少!
回答by David Joyner
Generically, this is the missing vtable problem: C++ FAQ Lite 23.10.
通常,这是缺少的 vtable 问题:C++ FAQ Lite 23.10。
From the Internet Archive:
来自互联网档案馆:
If you get a link error of the form "Error: Unresolved or undefined symbols detected: virtual table for class Fred," you probably have an undefined virtual member function in class Fred.
The compiler typically creates a magical data structure called the "virtual table" for classes that have virtual functions (this is how it handles dynamic binding). Normally you don't have to know about it at all. But if you forget to define a virtual function for class Fred, you will sometimes get this linker error.
Here's the nitty gritty: Many compilers put this magical "virtual table" in the compilation unit that defines the first non-inline virtual function in the class. Thus if the first non-inline virtual function in Fred is wilma(), the compiler will put Fred's virtual table in the same compilation unit where it sees Fred::wilma(). Unfortunately if you accidentally forget to define Fred::wilma(), rather than getting a Fred::wilma() is undefined, you may get a "Fred's virtual table is undefined". Sad but true.
如果您收到“错误:检测到未解析或未定义的符号:Fred 类的虚拟表”形式的链接错误,则您可能在 Fred 类中有一个未定义的虚拟成员函数。
编译器通常会为具有虚函数的类创建一个称为“虚拟表”的神奇数据结构(这就是它处理动态绑定的方式)。通常你根本不需要知道它。但是如果你忘记为类 Fred 定义一个虚函数,你有时会得到这个链接器错误。
下面是细节:许多编译器将这个神奇的“虚拟表”放在定义类中第一个非内联虚拟函数的编译单元中。因此,如果 Fred 中的第一个非内联虚拟函数是 wilma(),编译器会将 Fred 的虚拟表放在它看到 Fred::wilma() 的同一编译单元中。不幸的是,如果您不小心忘记定义 Fred::wilma(),而不是得到 Fred::wilma() 未定义,您可能会得到“Fred 的虚拟表未定义”。悲伤但真实。
回答by Robin R
In my case it was a defined pure virtual method in a base class which was declared but not implemented in a derived class (and more specifically the first virtual method in the vtable), e.g.:
在我的例子中,它是基类中定义的纯虚方法,它在派生类中声明但未实现(更具体地说是 vtable 中的第一个虚方法),例如:
class Base
{
public:
virtual int foo() = 0;
virtual int bar() = 0;
};
class Derived : public Base
{
public:
Derived() {}
~Derived() {}
virtual int foo(); // <-- causes this obscure linker error
virtual int bar() {return 0;}
};
回答by Frank
The same error can occur when one forgets to put the class name in front of the definition of a method in the cpp file - like I just did. And it's not an xcode thing, I'm using cmake for building and gcc as compiler (as xcode typically does).
当忘记将类名放在 cpp 文件中方法定义的前面时,可能会发生同样的错误 - 就像我刚刚做的那样。这不是 xcode 的事情,我使用 cmake 进行构建,使用 gcc 作为编译器(就像 xcode 通常那样)。
回答by dev
For me it was an XCode thing, as I have the same project compiling fine.
对我来说,这是一个 XCode 的东西,因为我有相同的项目编译良好。
In my file Foo.h I had constructor and destructor that is implemented in .cpp file. But I also had another class which I had in Foo.h whose implementation was in Foo.h and not in .cpp. So I had to add the Foo.h file in XCode project -> Targets -> "TragetName" -> BuildSources and this issue was solved.
在我的文件 Foo.h 中,我有在 .cpp 文件中实现的构造函数和析构函数。但是我还有另一个类,我在 Foo.h 中有它的实现是在 Foo.h 中而不是在 .cpp 中。所以我不得不在 XCode 项目 -> Targets -> "TragetName" -> BuildSources 中添加 Foo.h 文件,这个问题就解决了。
Hope this helps.
希望这可以帮助。