Today, with C + + writes the neural network algorithm. I have found a very puzzling question, which is described in general, for example:
I have two classes ClassA and CLASSB, each with member variables A. b;
ClassA has a function like this: Functiona (const ClassB &CLASSB)
There is a similar function in CLASSB: functionb (const CLASSA&CLASSA)
a start code similar to this, but the error, the reason is very easy,classb when Functiona use CLASSB, CLASSB There is only a declaration, undefined .
Class ClassB; < advance declaration Classbclass classa{public:void Functiona (const ClassB &CLASSB); int A;}; void Classa::functiona (const ClassB &CLASSB) {int k = classb.b; < here is an error, using an undefined classb}class classb{public:void functionb (const ClassA *classa) {int k = classa->a;}; int b;}; void functionb (const ClassA &classa) {int k = CLASSA.A;} int main () {return 0;}
Since the problem is: CLASSB only have a declaration, undefined.
In order to solve the above problem, one thing needs to be done here:
is to implement the Functiona. Put it in the definition of CLASSB .
Class ClassB; < advance declaration Classbclass classa{public:void Functiona (const ClassB &CLASSB); int A;}; Class Classb{public:void functionb (const ClassA *classa) {int k = classa->a;}; int b;};/ < Functiona after the definition of ClassB, there is no problem. void Classa::functiona (const ClassB &CLASSB) {int k = classb.b; } void functionb (const ClassA &classa) {int k = CLASSA.A;} int main () {return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Duanxx C + + learning: Using classes without being defined causes and workarounds