Skip to main content
gamedev.net
🔒 Locked

Class forward declaration problem

Started by DMINATOR Mar 15, 2005 at 3:38 PM 7 replies 1.2k views
Original Post
DMINATOR
DMINATOR
Well I encountered a problem , Maybe someone could help me out ? What I want to do is to access other class variables or functions . Here is the code example I have came up with : C++

#include <stdio.h>

class A;
class B;


class A
{
public:
	int var1;

	void Change()
	{
       binst.var2 = 10;
	}
};
A ainst;




class B
{
public:	
	int var2;
	void Change()
	{
		ainst.var1 = 20;
	}
};
B binst;




int main()
{
	printf(" var1 = %d\n",ainst.var1);
	printf(" var2 = %d\n",binst.var1);

	//let's change and see new things
     ainst.Change();
	 binst.Change();

	printf(" var1 = %d\n",ainst.var1);
	printf(" var2 = %d\n",binst.var1);


	return 0;
}

The problem is in the A class it can't see the binst (binst' : undeclared identifier) declaring it before the A class , produce compiler error like : ('binst' uses undefined class 'B'). So what is the best way to solve it ? Thanks in advance.
snk_kid
snk_kid
Using class forward declarations is only part of solving cyclic dependencies the second part is to use a pointer/reference to one of the types and provide its definition in source file where-ever needed.
MENTAL
MENTAL
Don't inline your code.

class A{public:    void Change ();    int var;};class B{public:    void Change ();    int var;};A ainst;B binstvoid A::Change (){    binst.var = 555;}void B::Change (){    ainst.var = 556;}void main (){    ainst.Change ();    binst.Change ();}


I should point out now that having 2 classes that are dependant on each other is a bad design flaw to start off with. having them dependant on global variables of each other is even worse :).
matthias6S
matthias6S
Separate declaration from definition, and it should work.

#include <cstdio>  // and use the right headers!class A{public:	int var1;	void Change();};class B{public:		int var2;	void Change();};A ainst;B binst;void A::Change(){    binst.var2 = 10;}void B::Change(){    ainst.var1 = 20;}int main(){	printf(" var1 = %d\n",ainst.var1);	printf(" var2 = %d\n",binst.var2); // and use the correct members	//let's change and see new things     ainst.Change();	 binst.Change();	printf(" var1 = %d\n",ainst.var1);	printf(" var2 = %d\n",binst.var2); // and use the correct members	return 0;}


Cheers
~matt();
MENTAL
MENTAL
is there an echo in here? [grin]
DMINATOR
DMINATOR
Thanks for the help. Yep design is not my strong part :) C++ is really new to me as is OOP programming, so I am only forming the idea how to manage code more efficiently .

Are there other ways to solve this problem ?

I think I heard an echo somewhere?
MENTAL
MENTAL
Tight coupling (ie. this) is a pain in the arse. A simple solution is to have each class store a pointer to the global object which can be set via a SetClassBSource/SetClassASource function. This means that the objects are not ties to global variables, which will help reduce coupling (which is a Good ThingTM).

I suggest you get yourself a copy of Code Complete 2nd. Edition. This book has litteraly changed the way I work and program. It's one of the best general-purpose books on software construction out there and has very good definitions of these types of problems. It also has a gazillion (yes that many) book references at the back, and just about all of them are classics.

I believe someone in these forums said it best: "Live and die by Code Complete". I couldn't agree more :).
DMINATOR
DMINATOR
Thanks for suggestion. I would take a look at this book.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.