//Class1. H content is as follows:
The class CClass2;
The class CClass1
{
Public:
The class CInnerClass
{
};
Public:
CClass2 * pClass2=nullptr;//defined here right
};
//Class2. H content is as follows:
The class CClass1;
The class CClass2
{
Public:
CClass1 * pClass1=nullptr;//defined here right
CClass1: : CInnerClass * pInnerClass=nullptr;//error defined here, unable to compile
};
Thus cause cannot be defined in the CClass2 CClass1: : CInnerClass class pointer object, don't know if you have a solution?
CodePudding user response:
Class2. H forgetting contains class1. H header file? You lead statement defines the class1, but is not defined in class1 members, members of the directly defined in class2 class1 for private members, is a must see to it,CodePudding user response:
Can, as long as the build CClass2 instances in the source file # include "class1. H" can, if not yet, change the compiler ~ ~ ~CodePudding user response:
Are just in the source file contains the definition, also written in the source fileCodePudding user response:
Generally this kind of operation is to use a virtual class as, two classes are common and include this virtual class inheritanceCodePudding user response:
Header file contains 1 2 good, 1 does not contain 2, front under the class declaration class2, CPP file contains 2 header filesCodePudding user response:
To add aThe class CClass1: : CInnerClass;
Forward declaration is not line? Anyway, you this is a pointer,
CodePudding user response:
The first is because contain each other, so can't use the include,I consider it a class CClass1: : CInnerClass; But this compilation will be wrong
CodePudding user response:
I use on VS2015, is your code, normal use,Class1
# pragma once
The class CClass2;
The class CClass1
{
Public:
CClass1 () {printf (" CClass1 Constructor \ n "); }
~ CClass1 () {printf (" CClass1 Destructor \ n "); }
Public:
The class CInnerClass
{
Public:
CInnerClass () {printf (" CInnerClass Constructor \ n "); }
~ CInnerClass () {printf (" CInnerClass Destructor \ n "); }
};
Public:
CClass2 * pClass2=nullptr;//defined here right
};
Class2
# pragma once
# include "Class1. H"
//Class2. H content is as follows:
The class CClass1;
The class CClass2
{
Public:
CClass2 () {printf (" CClass2 Constructor \ n "); }
~ CClass2 () {printf (" CClass2 Destructor \ n "); }
Public:
CClass1 * pClass1=nullptr;
CClass1: : CInnerClass * pInnerClass=nullptr;
};
The main
#include
# include "Class1. H"
# include "Class2. H"
Int main ()
{
CClass1 class1.
CClass2 class2;
return 0;
}
CodePudding user response:
Not to say that, in front of the two. H do not quote # include each other, in establishing CClass2 instances in the source file # include "class1. H" can (of course also should quote class2. H)CodePudding user response:
Why not template, a header file write definition, can only define is CPP?CodePudding user response:
According to zjq9931 reply, I found that, in accordance with the original approach, indeed can be compiled through, so it should be two classes have inner classes, will appear this problem, the problem is, indeed, I have written in the application of problem, because the program is more complex, now there are many types of class definition, so there is this kind of phenomenon, a temporary solution, encounter this kind of situation can only cancel the class definition in the class, but the program structure doesn't look perfect) :The following is to modify the class definition
//Class1. H content is as follows:
The class CClass2;
The class CClass2: : CClass2InnerClass;//compile by
The class CClass1
{
Public:
CClass1 ()
{
}
~ CClass1 ()
{
}
Public:
The class CClass1InnerClass
{
Public:
Long aaa=10;
};
Public:
CClass2 * pClass2=nullptr;//defined here right
CClass2: : CClass2InnerClass * pClass2Inner=nullptr;
};
//Class2. H content is as follows:
The class CClass1;
The class CClass1: : CClass1InnerClass;//this cannot compile
The class CClass2
{
Public:
The class CClass2InnerClass
{
Public:
Long aaa=5;
};
Public:
CClass2 ()
{
}
~ CClass2 ()
{
}
Public:
CClass1 * pClass1=nullptr;
CClass1: : CClass1InnerClass * pInnerClass=nullptr;
};
Thus a reference each other class in the class, a can define internal class pointer object, don't know if you have a solution?
CodePudding user response:
Why do you so shaft ~ ~ ~//Class1. H content is as follows:
The class CClass2;
The class CClass1
{
Public:
The class CInnerClass
{
};
Public:
CClass2 * pClass2=nullptr;//defined here right
};
//Class2. H content is as follows:
The class CClass1;
The class CClass2
{
Public:
CClass1 * pClass1=nullptr;//defined here right
CClass1: : CInnerClass * pInnerClass=nullptr;//here also normal
};
The test program:
#include
# include "class1. H"
# include "class2. H"
Int main ()
{
CClass2 c2.
Printf (" % p \ n ", c2. PInnerClass=0).
return 0;
}
CodePudding user response:
I use visual studio 2019 tested, if the parties have each other in the class and class in class pointer object, is not the first # include the above reference each other, then the actual CClass1 class1.CClass2 class2;
CodePudding user response:
null