Circular dependencies of declarations

前端 未结 6 1167
眼角桃花
眼角桃花 2021-01-19 05:02

I am trying to implement example of visitor pattern, but I have trouble with circular dependecies of declarations of classes. When I do forward declaration of class Visitor,

相关标签:
6条回答
  • 2021-01-19 05:40

    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.

    0 讨论(0)
  • 2021-01-19 05:46
    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);
    }
    
    0 讨论(0)
  • 2021-01-19 05:46

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

    0 讨论(0)
  • 2021-01-19 05:49

    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.

    0 讨论(0)
  • 2021-01-19 05:59

    When you forward declaration, C++ compiler knows that there's a user defined type of this kind but it doesn't know about it's data members and methods. In order to use the full feature of this user defined type you need to include it's header file where are located all it's methods and data members before use them otherwise you just make the forward declaration and use it's methods and data members where it's header file is included. In your case you are calling the visit() method of the forward declared Visitor class, this way you inform the compiler that there's a Visitor data type, but the compiler doesn't know yet about visit() method. In order to solve this you have to remove the forward declaration and put the Visitor definition on the top of all classes. You'll have something like this

    #include <cstdio>
    #include <vector>
    
    using namespace std;
    class England;
    class Russia;
    class Visitor {
      public:
        void visit(const England *e) const {
          printf("Hey, it's England!\n");
        }
    
        void visit(const Russia *r) const {
          printf("Hey, it's Russia!\n");
        }
    };
    
    class Land {
      public:
        virtual void accept(const Visitor *v);
    };
    
    class England : public Land {
      public:
        void accept(const Visitor *v) {
          v->visit(this);
        }
    };
    
    class Russia : public Land {
      public:
        void accept(const Visitor *v) {
          v->visit(this);
        }
    };
    ...
    
    0 讨论(0)
  • 2021-01-19 06:00

    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;
    };
    
    0 讨论(0)
提交回复
热议问题