Duanxx C + + learning: Using classes without being defined causes and workarounds

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.