May 30, 2013 at 7:40pm UTC
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 May 30, 2013 at 7:41pm UTC
May 30, 2013 at 7:54pm UTC
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.
May 30, 2013 at 8:08pm UTC
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 May 30, 2013 at 8:10pm UTC
May 30, 2013 at 8:35pm UTC
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 May 30, 2013 at 8:35pm UTC
May 30, 2013 at 8:59pm UTC
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 May 30, 2013 at 9:07pm UTC
May 30, 2013 at 9:16pm UTC
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 May 30, 2013 at 9:19pm UTC
May 30, 2013 at 9:27pm UTC
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 May 30, 2013 at 9:30pm UTC
May 30, 2013 at 9:34pm UTC
I already wrote you what is the problem.
May 30, 2013 at 9:39pm UTC
That is what I changed it to. But I get that error I just mentioned.
May 30, 2013 at 9:54pm UTC
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 May 30, 2013 at 9:56pm UTC
May 30, 2013 at 10:23pm UTC
And what is the error? Show where you call the function.
May 31, 2013 at 8:46am UTC
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 ();
May 31, 2013 at 6:10pm UTC
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.