Two class defintions that both reference each other.



class firstclass
{

firstclass& returnplusclass(Secondclass second)
{
//plus logic goes here
return *this;
}


};

class secondclass
{
secondclass& returnplusclass(Firstclass first)
{
//plus logic goes here
return *this;
}
};


Problem is the first class annot use the second's class defintion.
How can I solve this problem, as both classes must pass each other as arguments.
The classes don't actually need each other; the function just needs both. I suggest declaring it globally with two arguments:
firstclass returnplusclass(Firstclass first, Secondclass second) { ... }
Or you can pass by reference and use forward declaration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class secondclass;

class firstclass
{
	firstclass& returnplusclass(const secondclass& second);
};

class secondclass
{
	secondclass& returnplusclass(const firstclass& first);
};

firstclass& firstclass::returnplusclass(const secondclass& second)
{
	//plus logic goes here
	return *this;
}

secondclass& secondclass::returnplusclass(const firstclass& first)
{
	//plus logic goes here
	return *this;
}
Last edited on
The forward declartion solved my problem. Thank you.
Topic archived. No new replies allowed.