C + + A class contains a class B pointer, Class B contains a class A pointer to the case

Source: Internet
Author: User

#include <stdio.h>classBclass;classaclass{ Public: Friend Bclass; voidfunc () {PB-FUNCB (); }    intdata; Bclass*PB;};classbclass{ Public: Friend AClass; voidFUNCB () {p-func (); } AClass*p; intdata;};intMain () {return 0;}

Compilation Result:

1>------Started Build: Project: ABC, configuration: Debug Win32------
1> main.cpp
1>d:\cpptest\abc\abc\main.cpp: Error C2027: "Bclass" is used with undefined type
1> D:\cpptest\abc\abc\main.cpp (5): See Declaration of "Bclass"
1>d:\cpptest\abc\abc\main.cpp: Error C2227: The left side of "->FUNCB" must point to class/struct/union/generic type

Class A contains a pointer to Class B, and the method calls the Class B method, Class B contains a class of pointers, also called a method, did not take half a day

Find the Great God, said: Although you affirm the class B, but, the compiler just know there is this thing, and there is no way to implement, to put separate, header file one, CPP a

 class   Bclass;  class   aclass{ public      void   Func ();     int   data; Bclass  * PB;};  class   bclass{ public      void   Funca () {p ->func ();    } aclass  * P;  int   data;};  

Put the implementation in the CPP

#include"Aclass.h"void  Aclass::func () {    PB-andFunca ();}

Perfect solution.

Inspire C + + to write things, or to separate the class into two inside, the first file, CPP a

C + + A class contains a class B pointer, Class B contains a class A pointer to the case

Related Article

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.