Reputation: 775
To begin, I know that there are a lot of questions about this but I didn't find a solution for this, so I decided to write it.
I'm working on a C++ project and I need to use polymorphism. In order to solve my problem, I simplified it into two classes: Parent and Child. The Parent has only virtual methods, and the Child has to implement them. The only class that will be instantiated is the Child. So, here is the code:
Parent.hh
namespace ParentNamespace {
class Parent {
public:
Parent();
~Parent();
virtual int Start() = 0;
};
}
Parent.cc
#include "Parent.hh"
using namespace ParentNamespace;
namespace ParentNamespace {
Parent::Parent(){}
Parent::~Parent(){}
}
Child.hh
#include "Parent.hh"
namespace ChildNamespace {
class Child : public ParentNamespace::Parent {
public:
Child();
~Child();
int Start();
};
}
Child.cc
#include "Child.hh"
namespace ChildNamespace {
Child::Child(){}
Child::~Child(){}
int Start(){
return 0;
}
}
It compiles without errors (it produces .o files), but when it has to link them, it shows this error:
In function
ChildNamespace::Child::Child(): Child.cc:8:
undefined reference to vtable forChildNamespace::Child
I've tried with the responses from the other questions, but no success. I suppose that I can't see something simple, so please help!
Thanks in advance.
Upvotes: 2
Views: 3774
Reputation: 508
You need to implement the pure virtual function, add Child:: to Start method In Child.cc
#include "Child.hh"
namespace ChildNamespace {
Child::Child(){}
Child::~Child(){}
int Child::Start(){
return 0;
}
}
I hope this help you
Upvotes: 1