MichaelB
MichaelB

Reputation: 1362

C++, missing vtable compilation error

I created an interface (only .hpp file):

struct IA
{
public:
    virtual void foo();
};

and a class implementing this interface (.hpp)

class A : public IA
{
    virtual void foo();
}

and .cpp

void A::foo(){...}

this compiles without a problem, but when I use

IA a;

in another file I get this compilation error: "vtable for IA, referenced from: IA::IA() in libSomeOtherFile.a(SomeOtherFile.o) Note: a missing vtable usually means the first non-inline virtual member function has no definition" anyone know why and how do I fix that ?

Upvotes: 0

Views: 136

Answers (2)

nneonneo
nneonneo

Reputation: 179442

If your goal is to make an interface, then you should not be creating an instance of the interface type directly. Use a pointer instead:

IA *a = new A();

Additionally, if it isn't meant to be instantiated then you should make foo pure-virtual in IA:

/* In IA */
virtual void foo() = 0;

Upvotes: 4

user2209008
user2209008

Reputation:

Do this instead.

struct IA
{
public:
    virtual void foo() { }
};

IA:foo is not a pure virtual function and thus must have an implementation if it's to be overridden in child classes.

Upvotes: 1

Related Questions