Thread: Interdependent Classes

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Unhappy Interdependent Classes

    I have a complicated problem that boils down to this:

    2 classes that work together, need to have pointers to one another and complete access to all members and function.

    the challenge lies in the fact that they cannot both be created at the same time. For example, a function in one class, uses the pointer to the other class to access a function or variable that doesn't technically exist yet. In case you're confused, here's a quick example of what i'm talking about:

    Code:
    class B; //a prototype so that class A knows class B exists;
    
    class A
    {
    public:
    	B *classB; //pointer to class B
    	void fxnA();
    	void write();
    };
    void A::fxnA()
    {
    	*classB->write();
    }
    void A::write()
    {
    	cout<<"A"<<endl;
    }
    
    
    class B
    {
    public:
    	A *classA; //pointer to class A
    	void fxnB();
    	void write();
    };
    void B::fxnB()
    {
    	*classA->write();
    }
    void B::write()
    {
    	cout<<"B"<<endl;
    }
    When compiled the compiler throws "use of undefined type B" at:
    *classB->write(); ( in fxnA() )

    If the order of the classes is switched, fxnB() will throw the same error about A.


    hopefully I'm just ignorant and forgetting something.

    Help??

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    It should be working, but then again I'm getting rusty - been programming Flash.

    One problem is it should be classB, not *classB, and classA, not *classA, you are using -> to show that its a pointer.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Move the definition of fxnA below the definition of class B.

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah, it should look like this:
    Code:
    class B;
    
    class A
    {
    public:
    	B *classB;
    	void fxnA();
    	void write();
    };
    void A::write()
    {
    	cout<<"A"<<endl;
    }
    
    
    class B
    {
    public:
    	A *classA; 
    	void fxnB();
    	void write();
    };
    void B::fxnB()
    {
    	classA->write();
    }
    void B::write()
    {
    	cout<<"B"<<endl;
    }
    void A::fxnA()
    {
    	classB->write();
    }
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In the case of interdependant classes its better to split it between header and source files. In the headers you put only the member variables, and prototype the member functions. In the source you define all the member functions. This will save you the above problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM