I haven't write complex C++ program in a long time but if I remember correctly, you should out the skeleton of those classes in .h
file with the same name with this .c
file. Then include it into this .c
file.
Hope this helps.
I haven't write complex C++ program in a long time but if I remember correctly, you should out the skeleton of those classes in .h
file with the same name with this .c
file. Then include it into this .c
file.
Hope this helps.
class Visitor;
class England : public Land {
public:
void accept(const Visitor *v); // Only declaration
};
// Define Visitor
class Visitor {
//...
};
// Now implementation
void England::accept(const Visitor *v) {
v->visit(this);
}
Give all class type declaration before its usage .. i think it would work.
Alexy gave one part of the answer already.
However, if you're not going to implement accept for Land then you need:
class Land {
public:
virtual void accept(const Visitor *v)= NULL;
};
Alexey Malistov's answer does solve your problem. It just also exposes the next problem.
The gcc compiler is complaining about the vtable (which is used for classes with virtual functions, amongst other things). The rules it uses are documented (see docs):
If the class declares any non-inline, non-pure virtual functions, the first one is chosen as the "key method" for the class, and the vtable is only emitted in the translation unit where the key method is defined.
Now, Alexey's version of your class defines the non-inline, non-pure virtual function accept. So, gcc defers instantiation of the Land vtable until it sees the definition of Land::accept. Add that, and see if it works. Or, as Nicholaz says, just make it a pure virtual.
Well, I do not want to "solve" the problem. I want to understand what is wrong and why
Get used to seperating declarations from definitions. Except for the special case of templates, C++ tends to work better this way.