friendship not being recognized

Hello, I'm learning about the friend function but I can't seem to get it work though. Can someone tell me what I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Auxi.h
#ifndef AUXI_H
#define AUXI_H

typedef unsigned short int USHORT;

class Auxi
{
private:
	static double budget;
public:
	Auxi() { budget = 0; }
	static void addBudget(double);
};

#endif

//Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include "Auxi.h"

class Example
{
private:
	static USHORT myNum;

public:
	Example() {}
	Example(USHORT n) { myNum = n; }
	
	static void setValue(USHORT);
        static int getValue();

	friend void Auxi::addBudget(double);
};

#endif

//Auxi.cpp
#include "Auxi.h"
#include "Example.h"

double Auxi::budget = 0;

void Auxi::addBudget(double b)
{
	budget += b;
}

//Example.cpp
#include "Example.h"

USHORT Example::myNum = 0;

void Example::setValue(USHORT n)
{
	myNum = n;
}

int Example::getValue()
{
	return myNum;
}

//main.cpp
#include <iostream>
#include "Example.h"

int main()
{
	Example obj1;

        obj1.setValue(2);
        std::cout << obj1.getvalue();

         //This line does not work. It doesn't even recognize that addBudget is a friend.  
        obj1.addBudget(64);

	return 0;
}
Last edited on
Friends are not members. Friends merely have access to internals of a class.

As friend the Auxi::addBudget() could modify obj1.myNum. Alas, there is no way to tell Auxi::addBudget() which Example to touch.
I'm not quite sure I follow you. I don't understand what you mean by Auxi::addBudget() being able to modify obj1.myNum. How is that even possible?

I made this from looking at an example in a book I'm reading. The code isn't exactly the same but it's similar in the way it's executed.

The book's example, Aux::addBudget(), was able to tell which Example to touch.

Thanks for the reply!
Last edited on
This syntax

obj1.addBudget(64);

is invalid for calling the friend function. Function addBudget is not a member of class Example. So it my not be called using an object of the class.
Moreover it is not clear why you defined it as a friend function of class Example when the function does nothing with objects of class Example. Why is it defined as a friend of Example?!
Last edited on
I'm just simulating the example I found in my book. Clearly, I'm not understanding it. I thought once you made addBudget a friend, Example could access it, but that is not the case as you have stated.

Can you tell me how my book did this then?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//auxil.h
#ifndef AUXIL_H
#define AUXIL_H

class Aux
{
private:
         double auxBudget;
public:
         Aux() { auxBudget = 0; }
         void addBudget(double);
         double getDivBudget() { return auxBudget; }
};
#endif

//budget3.h
#ifndef BUDGET3_H
#define BUDGET3_H
#include "auxil.h"

class Budget
{
private:
         static double corpBudget;
         double divBudget;
public:
         Budget() { divBudget = 0; }
         void addBudget(double b)
                  { divBudget += b; corpBudget += divBudget; }
         double getDivBudget() { return divBudget; }
         static double getCorpBudget() { return corpBudget; }
         static void mainOffice(double);

         friend void Aux::addBudget(double);
};
#endif

//auxil.cpp
#include "auxil.h"
#include "budget3.h"

void Aux::addBudget(double b)
{
         auxBudget += b;
         Budget::corpBudget += auxBudget;
}

//budget3.cpp
#include "budget3.h"

double Budget::corpBudget = 0;

void Budget::mainOffice(double budReq)
{
         corpBudget += budReq;
}

//main.cpp
#include <iostream>
#include <iomanip>
#include "budget3.h"
using namespace std;

int main()
{
         const int N_DIVISIONS = 4;
         Budget divisions[N_DIVISIONS];

         for (int count = 0; count < N_DIVISIONS; count++)
         {
                  double bud;
                  cout << "Division " << (count + 1) << ": ";
                  cin >> bud;
                  divisions[count].addBudget(bud); //This line here works.
                  cout << "Division " << (count + 1) << "'s auxiliary office: ";  
         }

         return 0;
}
Last edited on
It is not the Example that may access the friend function. It is the friend function that may access private and protected members of the Example.

EDIT: In your example the friend function changes private static member of class Budget.

1
2
3
4
5
void Aux::addBudget(double b)
{
         auxBudget += b;
         Budget::corpBudget += auxBudget;
}
Last edited on
If what you say is true, why can't I do this in Aun.cpp:

1
2
3
4
5
void Auxi::addBudget(double b)
{
	budget += b;
        Example::myNum += budget; //Says myNum is inaccessible.
}


I noticed the int to double and fixed that, but still inaccessible.
Last edited on
I already wrote you what is the problem.
That is what I changed it to. But I get that error I just mentioned.
Show your updated code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Auxi.h
#ifndef AUXI_H
#define AUXI_H

class Auxi
{
private:
	static double budget;
public:
	Auxi() { budget = 0; }
	static void addBudget(double);
};

#endif

//Example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include "Auxi.h"

class Example
{
private:
	static double myNum;

public:
	Example() {}
	Example(double n) { myNum = n; }
	
	static void setValue(double);
	static double getValue();

	friend void Auxi::addBudget(double);   
};

#endif

//Auxi.cpp
#include "Auxi.h"
#include "Example.h"

double Auxi::budget = 0;

void Auxi::addBudget(double b)
{
	budget += b;
	Example::myNum += budget; //Error on myNum     
}

//Example.cpp
#include "Example.h"

double Example::myNum = 0;

void Example::setValue(double n)
{
	myNum = n;
}

double Example::getValue()
{
	return myNum;
}
Last edited on
And what is the error? Show where you call the function.
Error I get is in Auxi.cpp:

1
2
//Auxi.cpp
member "Example::myNum" is inaccessible 


In main.cpp, I call it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//main.cpp
#include <iostream>
#include "Example.h"

int main()
{
	Example obj1;
        Auxi bud;

        obj1.setValue(2);
        std::cout << obj1.getvalue();

        bud.addBudget(33); //No error here. 

	return 0;
}

I still don't understand how the book example was able to call addBudget() with a Budget object. You say it is because the function is changing the Budget member, but I still can't do it in my program even though I am doing the same thing. Unless I misunderstood again.
Your module main.cpp must include header "Auxi.h"
Also the code in main has a typo. Instead of

std::cout << obj1.getvalue();

shall be

std::cout << obj1.getValue();
That still didn't fix anything. I get the same error and still can't imitate the book example.

While I included "Auxi.h" in main.cpp, I don't understand why I needed to. Example.h already includes it.

Thanks for helping me with everything so far.
Topic archived. No new replies allowed.