Thursday, March 8, 2012

NOTE: a missing vtable usually means the first non-inline virtual member function has no definition

When you encounter this compilation error:

Michael-Buens-MacBook:CppProj Michael$ gcc b.cpp
Undefined symbols for architecture x86_64:
  "operator new(unsigned long)", referenced from:
      _main in ccbv8mjf.o
      Alpha::doStuff(char) in ccbv8mjf.o
      Beta::doStuff(char) in ccbv8mjf.o
  "vtable for __cxxabiv1::__class_type_info", referenced from:
      typeinfo for Alphain ccbv8mjf.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for __cxxabiv1::__si_class_type_info", referenced from:
      typeinfo for Betain ccbv8mjf.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
collect2: ld returned 1 exit status


You are holding it wrong, use g++ :-)

Michael-Buens-MacBook:CppProj Michael$ g++ b.cpp


b.cpp (Liskov principle):
#include <cstdio>

class Alpha
{
 public: virtual Alpha* doStuff(char c)
 {
  printf("From Alpha\n");
  return new Alpha();
 }
};

class Beta : public Alpha
{
 public: virtual Beta* doStuff(char c)
 {
  printf("From Beta\n");
  return new Beta();
 }
};

int main()
{

 Alpha* a = new Beta();
 Alpha* x = a->doStuff('x');
 
 Beta* b = new Beta();
 Beta* y = b->doStuff('x'); // no need to cast to Beta
 
 
 return 0;
}

No comments:

Post a Comment