Reputation: 2291
I have implemented an interface:
class ISolution
{
public:
virtual ~ISolution() = 0;
virtual void resultat() = 0;
};
and some derived classes from it:
class SolX : ISolution
{
private:
int member;
MyClass myOb;
public:
SolX(const MyClass& inOb) : myOb(inOb) {}
~SolX() {}
void resultat()
{
// the body
}
};
When I compile it I get errors like:
/proj/CSolution1.cpp:4: undefined reference to `ISolution::~ISolution()'
/proj/CSolution2.cpp:4: undefined reference to `ISolution::~ISolution()'
CMakeFiles/proj.dir/CSolution2.cpp.o:/proj/CSolution2.cpp:12: more undefined references to `ISolution::~ISolution()' follow
I am using CMake and Linux Ubuntu.
I cannot figure out what is the problem. Is there something that I have missed? Does myOb
create the problems?
P.S.: The line where is the error is at the definition of the constructor.
Upvotes: 0
Views: 2515
Reputation: 12057
The problem is
virtual ~ISolution() = 0;
the destructor is not implemented. Try
virtual ~ISolution() { }
instead.
This is needed even when only instances of the derived class are created, because destructors are special. In a normal function, the base class implementation needs to be called explicitly, like
void Derived::Foo() {
Base::Foo();
....
}
but for a destructor, this is always done automatically.
Upvotes: 3
Reputation: 311088
According to the C++ Standard
9 A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined.
So you have to define the destructor of the base class because it is called from the destructor of the derived class.
Upvotes: 3
Reputation: 101494
A pure virtual
destructor still needs a base class implementation. At least it does in this case, because without it you can't delete
the object.
Although not intuitive, this is a fairly common paradigm when implementing polymorphic base classes.
In some file, perhaps isolutiuon.cpp
, implement:
ISolution::~ISolution()
{
}
Upvotes: 1