0

I am relatively new to c++ and having an implementation like below:

class A {
  virtual void func1() = 0;
};

class B : A {
  void func1() override{
    // imlement func1 here
  }
  virtual func2() = 0;
}

class C : B {
  void func2() override{
    // implement func2 here;
  }
}

These classes are packaged as their own binaries and can be build individually.

B builds successfully, but when I build C, it gives an 'Undefined reference to vtable' error. To get rid of it, I have to implement func1() in C.

But for func1(), I want the implementation in B (because in my project, B is an existing legacy class) not in C. Is there a way to do it?

Thanks

user2116990
  • 51
  • 1
  • 6
  • 5
    `packaged as their own binaries` This needs more details. If you mean some sort of shared/dynamic libraries, then you must somehow "export" the classes, and the way to do that is platform and compiler specific. – dxiv Sep 21 '20 at 04:01
  • Does this answer your question? [Undefined reference to vtable](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable) – David C. Rankin Sep 21 '20 at 04:02

1 Answers1

0

I like @David C. Rankin's reference. You should try adding in a virtual default destructor in at least classes A and B (and maybe C as well). Also, in your code snippet, you were missing a few things (like a return type on B's func2 definition, etc.). Here's a cleaned up example with the virtual default destructors:

class A {
public:
    virtual void func1() = 0;
    virtual ~A() = default;
};

class B : A {
public:
    void func1() override {
        // implement func1 here;
    }
    virtual void func2() = 0;
    virtual ~B() = default;
};

class C : public B {
public:
    void func2() override {
        // implement func2 here;
    }
    virtual ~C() = default;
};
sfb103
  • 264
  • 1
  • 7