Friend functions - variable out of scope

closed account (GhR4GNh0)
I'm trying out friend functions not in just one source file to see how it works but I'm getting an error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* ----- ClassOne.h ----- */
#ifndef CLASSONE_H_
#define CLASSONE_H_

#include "classTwo.h" 

using namespace std;

class ClassOne {
	private:
		int m_a;
		int  m_b;
	public:
		ClassOne(int a, int b);
		void printValuesOne();
		friend void ClassTwo::twoPrintsOne();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* ----- ClassOne.cpp ----- */
#include <iostream> 
#include "classOne.h"

using namespace std;

ClassOne::ClassOne(int a, int b) {
	m_a = a;
	m_b = b;
}

void ClassOne::printValuesOne() {
	cout << "m_a: " << m_a << " " << "m_b: " << m_b << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* ----- ClassTwo.h ----- */
#ifndef CLASSTWO_H_
#define CLASSTWO_H_

using namespace std;

class ClassTwo {
	private:
		int m_c;
		int m_d;
	public:
		ClassTwo(int c, int d);
		void printValuesTwo();
		void twoPrintsOne();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* ----- ClassTwo.cpp ----- */
#include <iostream> 
#include "classTwo.h"

using namespace std;

ClassTwo::ClassTwo(int c, int d) {
	m_c = c;
	m_d = d;
}

void ClassTwo::printValuesTwo() {
	cout << "m_c: " << m_c << " " << "m_d: " << m_d << endl;
}

void twoPrintsOne() {
	cout << "ClassTwo: " << "m_a:" << m_a << " " << "m_b: " << m_b << endl;
}


Basically, I just want to print out the private members of ClassOne using ClassTwo's friend function twoPrintsOne().

The error is in classTwo.cpp and it says that m_a and m_b in the twoPrintsOne function are not declared in this scope. I am not sure how to fix this.
Well, twoPrintsOne needs to be passed the actual object of ClassOne that it wants to print the private variables of. You can't print m_a and m_b of an object ClassTwo if there is no ClassOne to print. So, just re-write it as accepting a parameter of type ClassOne, and just prefix m_a and m_b with whatever name you give it in the pass.
closed account (GhR4GNh0)
Silly me, I should have saw something like that. I corrected that function now but it seems to be another problem; the compiler says that ClassTwo.h does not recognise ClassOne as a type.
1
2
3
4
5
In file included from classOne.h:10:0,
                 from classOne.cpp:8:
classTwo.h:21:27: error: ‘ClassOne’ does not name a type
   void twoPrintsOne(const ClassOne& c);
                           ^

So the problem is the include of classTwo.h in classOne.h? If so, how do I get around that?
It depends on how you want to go about it. Because they both depend on the other (which, by the way, is a sign of a design flaw; but since this is just a trial-and-error of friends it's no issue), you have a few options:

-Make the entirety of ClassTwo a friend of ClassOne instead of just the function, removing the #include requirement and letting you avoid the circular inclusion.

-Have ClassOne have a friend function that, too, is a friend of ClassTwo. That way, you can call the function that, in turn, would call the right function in ClassTwo. This way, all you need is a forward declaration in the header and the #include in the implementation file.

Of course, both of those options are not particularly the best- really, this is a demonstration that friend functions have very specific uses. Usually, if you can avoid a friend function, do so- inheritance tends to work much better.
Topic archived. No new replies allowed.