Jump to content
  • Advertisement
Sign in to follow this  
DMINATOR

Class forward declaration problem

This topic is 4907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Don't inline your code.


class A
{
public:
void Change ();
int var;
};

class B
{
public:
void Change ();
int var;
};

A ainst;
B binst

void 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 :).

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!