I have the code below:
// IBase.h
#include <iostream>
class IBase{
public:
virtual string getId();
};
// IBase.cpp
#include "IBase.h"
string IBase::getId(){};
// Base.h
#include <iostream>
#include <string>
#include "IBase.h"
using namespace std;
class Base : public IBase{
protected:
string id;
public:
Base(string _id);
string getId();
};
// Base.cpp
#include "Base.h"
Base::Base(string _id){
id = _id;
}
string Base::getId(){
return id;
}
// Base2.h
#include <iostream>
#include <string>
#include "Base.h"
using namespace std;
class Base2 : public Base{
public:
Base2(string _id);
string getId();
};
// Base2.cpp
#include "Base2.h"
Base2::Base2(string _id) : Base(_id){};
And i got the Undefined reference to `vtable in for Base2 when i compile the project! Is this i miss something?
Undefined reference to vtable usually indicates that the first virtual function(where the vtable is usually stored) was not linked.
As no definition for Base2::getId
is provided(the first virtual function), no vtable gets linked.
Either add a definition for Base2::getId
or remove the declaration for the function in the class definition.