"declaration without type" Why does this error? Communication between classes

 

Hello everyone. Sorry for my English.

This code gives me compile error.

'Class2' - declaration without type Class1.mqh

class Class1
{
   private:
      Class2         class2;
   public:
                     Class1(){};
                    ~Class1(){};
};


class Class2
{
   private:
      Class1         class1;
   public:
                     Class2(){};
                    ~Class2(){};
};


I need to communicate two classes because the class one has elements that need class two and one class, need to access the elements of class two.

Not possible in MQL4, make cross-references between classes?

I do not understand. Any help?. Thanks

 
estados:

Hello everyone. Sorry for my English.

This code gives me compile error.

'Class2' - declaration without type Class1.mqh


I need to communicate two classes because the class one has elements that need class two and one class, need to access the elements of class two.

Not possible in MQL4, make cross-references between classes?

I do not understand. Any help?. Thanks

class Class2;

class Class1
{
   private:
      Class2         *class2;
   public:
                     Class1(){};
                    ~Class1(){};
};


class Class2
{
   private:
      Class1         *class1;
   public:
                     Class2(){};
                    ~Class2(){};
};
 

Thank you very much angevoyageur. Testing, I found that if used includes the declaration of the class should come first, that no error.

class Class2;

#include <Class2.mqh>

class Class1;

#include <Class1.mqh>
 
If you have mutually communicating classes like that, it should be redone. In a court the defendant and plaintive don't talk to each other, they talk to the judge. Put your two classes inside a controlling class and eliminate the dependency.
 

@WHRoeder

Just a thought...what about creating a base class with static class members (variables) and then create an new class derived from the base class to doing whatever needs to be done? Wouldn't that give the derived class the ability to both see and change the inherited data members of the base class? Moreover, wouldn't that also give all classes derived from the base class the ability to see (and, if needed, change--maybe depending on the abilities of the derived class) the data contained in the variables of the base class?

To be honest, while I'm not quite new to concepts of OOP, it has been a few years since I have implemented them, so like allot of people here, I'm attempting to refresh myself. :)

 

@WHRoeder, @Thirteen

I gave an answer to the opening poster to resolve his issue, but I agree with you. I can't think a situation where such design is the best solution and in my opinion it's certainly a bad practice to work this way. Though it could be interesting to hear @estados about the purpose of his implementation.

 
Really as you say, it's like I'm doing. Try to simplify the technical problem I had, to be easier to understand.
 

I had a similar challenge, though I'm not sure it corresponds to the OP requirements.

My classes were of the same structure and conceptually on the same level. In that case, using an interface solved it elegantly.