Shardul Upadhyay
Shardul Upadhyay

Reputation: 417

Simple Inheritance Gets Vtable Error

So I am trying to compile this simple code:

// In Test.h
#include <iostream>
using namespace std;

class A{

public:
    virtual string f (string&);
};

class B : public A{

public:
    B (string);
    string f (string&);
};

// In Test.cpp
#include <iostream>
#include "Test.h"
using namespace std;


B :: B (string row){
    cout << "HERE";
}

string B :: f (string& x){
    cout << "Test";
}

It seems simple enough but I keep getting a Undefined reference to 'vtable for A' error (compiler is MINGW with Eclipse IDE). When I take out the constructor implementation for B, the code compiles fine. What am I missing or is this a linker error?

Upvotes: 0

Views: 211

Answers (1)

CygnusX1
CygnusX1

Reputation: 21808

I think you either have to make A::f(string&) abstract by writing = 0 at the end of the declaration, or actually provide an implementation of A::f

Since currently the virtual function A::f does not exist, the compiler cannot create a vtable for it (the virtual function table).

Upvotes: 1

Related Questions