1

Im' having problem with pure virtual methods and forward class declaration. The following program compiles, but will not link.

class SubClass;

class SuperClass {
    public:
    virtual SubClass& GetSomething() = 0;
};

class SubClass: public SuperClass {
    public:
    SubClass& GetSomething();
};


SubClass& SubClass::GetSomething()
{
    return *this;
}

int main()
{
    SuperClass *obj = new SubClass();
    SubClass result = obj->GetSomething();
    return 0;
}

I'm getting the following error:

Undefined symbols for architecture x86_64:
  "vtable for __cxxabiv1::__class_type_info", referenced from:
      typeinfo for SuperClass in classtest-56e822.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 SubClass in classtest-56e822.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "operator new(unsigned long)", referenced from:
      _main in classtest-56e822.o
  "___cxa_pure_virtual", referenced from:
      vtable for SuperClass in classtest-56e822.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What am I doing wrong?

user29809
  • 85
  • 7
  • 1
    I can't see anything obvious to cause that error, but you should make sure to mark the function in the derived class with `override`, so you can be sure there isn't a misspelling or something causing it. – John Ilacqua Dec 09 '18 at 08:48
  • 1
    How do you want to correctly `delete obj;`? Virtual classes always should provide a virtual detructor! Side note: you did not delete, so actually, you got a memory leak (only being cleaned up by OS - *if* you are on one that does - when your application exits). – Aconcagua Dec 09 '18 at 08:49
  • 5
    Which compiler and switches are you using? Using G++ on LInux I cannot reproduce the error. BTW: The virtual destructor is missing... – R Yoda Dec 09 '18 at 08:53
  • I tried to compile&run your code, but could not produce an error on my side. I am using MinGW on Windows. – Ozan Yurtsever Dec 09 '18 at 08:56
  • The error message comes from gcc on my Mac. Having the same problem within xcode (which also uses gcc). – user29809 Dec 09 '18 at 08:57
  • 5
    Xcode uses clang, not gcc. And it would tell you that the virtual destructor (that sometimes triggers the vtable being added) is missing. Add it. – Matthieu Brucher Dec 09 '18 at 09:00
  • 3
    Possible duplicate of [A missing vtable usually means the first non-inline virtual member function has no definition](https://stackoverflow.com/q/31861803/608639) and [c++ a missing vtable error](https://stackoverflow.com/q/15265106/608639) – jww Dec 09 '18 at 09:01
  • To be precise: Apple replaced gcc as default compiler with clang - and left a symlink from gcc to clang for compatibility. But you *could* explicitly install gcc on your system and replace the symlink to enforce GCC, if you wanted to. – Aconcagua Dec 09 '18 at 09:02
  • 2
    @jww: Really a duplicate? Other question did declare destructor, but not define it - this one does not declare at all, thus a (but non-virtual) default is generated... I'd say it is a seemingly marginal, but essential difference. – Aconcagua Dec 09 '18 at 09:04
  • @Aconcagua There are no duplicate indeed. And I cannot reproduce this error message on my system neither with clang nor with gcc. Certainly a bug of your compiler. – Oliv Dec 09 '18 at 10:37
  • There can only be one kind of `SubClass` for your `SuperClass` and you can't instantiate the `SuperClass `. What is the point then, of having two separate classes ? – Sid S Dec 09 '18 at 10:47
  • My compiler does not run into this issue. But, as a workaround, you can do this in a CPP file `SubClass& SuperClass::GetSomething() { throw Blah(); };` to generate the vtable in that translation unit. Even though it was declared pure virtual, you can still define an implementation for it. – Eljay Dec 09 '18 at 13:33

0 Answers0