Reputation: 303
Given following classes:
// myClass.hpp
class Child1;
class Child2;
class MyClass {
public:
virtual void doSomething (const MyClass&) const = 0;
virtual void doSomething (const Child1&) const = 0;
virtual void doSomething (const Child2&) const = 0;
};
// child1.hpp
#include "myClass.hpp"
class Child1 : public MyClass {
public:
virtual void doSomething (const MyClass&) const override;
virtual void doSomething (const Child1&) const override;
virtual void doSomething (const Child2&) const override;
};
// child2.hpp
#include "myClass.hpp"
class Child2 : public MyClass {
public:
virtual void doSomething (const MyClass&) const override;
virtual void doSomething (const Child1&) const override;
virtual void doSomething (const Child2&) const override;
};
The compiler gives me the errors:
undefined reference to 'Child1::doSomething(MyClass const&)'
The same error is printed for the other doSomething(..)
functions.
I'm sure there is some mistake in including files (I use include guards for every header-file!!). My questions are: Where do I have to include the corresponding files and where do I need forward declaration?
Upvotes: 0
Views: 1290
Reputation: 238
The error tells you that there is no reference to the whole Method.
In other words you need to define the Method in a *.cpp File or directly in the header. See for example this overview or this similar question. The answers tell you that is actual not a Compiler error but a Linker error.
Edit: As Hans Olsson Answer suggests, it could also be that you need to include your cpp files.
If you do not exactly know how the implementation will look like but want to know if you header-concept works you can implement them empty
virtual void doSomething (const MyClass&) const override {}
Or compiler your code with gcc with the flag "-c" which tells gcc enter to just do the compiling not the linking.
Upvotes: 1
Reputation: 12517
"Undefined reference" looks like a linker problem. Ensure that you have a child1.cpp including child1.hpp and that you compile and with it; and that child1.cpp define the override function in the correct way.
Upvotes: 1