vtable for .. referenced from compile error xcode
Asked Answered
E

5

19

I was getting the following error compiling an iPhone project:

"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?

Electrothermal answered 22/9, 2009 at 5:4 Comment(0)
E
41

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!

Electrothermal answered 22/9, 2009 at 5:5 Comment(4)
I agree, terrible error message. I had this because no implementation of virtual method in base class.Elaineelam
Thanks for answering this question -- it comes as the first google result for "vtable for referenced from" -- saved me a lot of time.Jillane
I'm getting the vtable error despite writing a body for the destructor. Is there anything else I could be missing?Cavetto
@Elaineelam -- I also got this error because of virtual method in base class with no implementation. It went away when I added a simple {} after the declaration.Schrick
D
14

Generically, this is the missing vtable problem: 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.

Donaghue answered 25/9, 2009 at 17:18 Comment(0)
R
7

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

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;}
};
Reisinger answered 28/1, 2013 at 15:22 Comment(0)
A
4

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

Ascender answered 1/2, 2010 at 19:36 Comment(0)
P
4

For me it was an XCode thing, as I have the same project compiling fine.

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.

Hope this helps.

Peroxide answered 1/3, 2012 at 6:4 Comment(1)
nowadays its called "Compile Sources" but worked perfectly for me!Cioban

© 2022 - 2024 — McMap. All rights reserved.