Inclusion problem

I have an inclusion template as follows:

/*
 * Class1.h
 */

#ifndef CLASS1_H_
#define CLASS1_H_

#include "Class2.h"

namespace Class1_namespace
{

class Class1
{
  Class2* Class2_ptr;
  void Class1_member()
  {
      (*Class2_ptr).Class2_method();
  }
};

}

#endif /* CLASS1_H_ */

/*
 * Class2.h
 */

#ifndef CLASS2_H_
#define CLASS2_H_

#include "Class1.h"

class Class2
{
    Class1_namespace::Class1 Class2_data;

public:
    void Class2_method(){};
};

#endif /* CLASS2_H_ */

/*
 * main.cpp
 */

#include "Class1.h"

int main()
{
    return 0;
}

However, this results in an error. "Class1_namespace" does not name the type. "

Is this error causing the ordering of my inclusions?

What are the possible solutions? I doubt forward declarations that solve my problem.

+3
source share
2 answers

Class1 should not include Class2.

If you have a mutual dependency (which you cannot - you can simply not include 2 in 1), you can usually solve it using forward declarations instead of inclusions.

For example, let's say that Class1 looked like this:

#include "Class2.h"

namespace Class1_namespace
{

    class Class1
    {
        Class2* class2;
    };

}

, include, :

class Class2;

namespace Class1_namespace
{

    class Class1
    {
        Class2* class2;
    };

}

.

+2

class1.h #include 2.h. - - .

0

Source: https://habr.com/ru/post/1758098/


All Articles