views:

283

answers:

5
A: 

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.

NawaMan
Well, I do not want to "solve" the problem. I want to understand what is wrong and why. But thanks.
Martin Kopta
+7  A: 
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);
}
Alexey Malistov
Am afraid that this doesn't solve the problem. Now I get lots of -- undefined reference to `vtable for Land', and undefined reference to `typeinfo for Land'.
Martin Kopta
Have you actually defined class Land anywhere? I think you probably wanted to make Land pure virtual:class Land { public: virtual ~Land() {} virtual void accept(const Visitor *v) = 0;};The error you're seeing is that GCC hasn't made the vtable for Land as you never defined any functions for it.
Matt G
Well, Alexey solved this problem, but new problem comes up: http://stackoverflow.com/questions/1748827/virtual-tables-are-undefined
Martin Kopta
A: 

Give all class type declaration before its usage .. i think it would work.

Ashish
+1  A: 

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;
};
Nicholaz
This problem has been separated from original question: http://stackoverflow.com/questions/1748827/virtual-tables-are-undefined
Martin Kopta
+1  A: 

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.

Useless