Friend Function

I'm supposed to create a function that adds the amount of dollars and cents, so I rewrote the code to include a friend function, and for some reason, I can't get to compile. It's really been frustrating me.

Here's my 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// P2_4a.cpp - This program adds money of two different people 
#include<iostream>
#include<cstdlib>
using namespace std;

class AltMoney 
{
	public: 
		AltMoney();
		AltMoney(int d, int c);
		int add (AltMoney d, AltMoney c);
	
		void display_money( ); 
	private: 
		int dollars; 
		int cents; 
		int add (get.m1, get.m2);
};

void read_money(int& d, int& c);

int main( ) 
{
	int d, c; 
	AltMoney m1, m2, sum;
	
	sum = AltMoney(0,0);
	
	read_money(d, c); 
	m1 = AltMoney(d,c); 
	cout << "The first money is:"; 
	m1.display_money();

	read_money(d, c); 
	m2 = AltMoney(d,c);
	cout << "The second money is:";
	m2.display_money();
	
	add (m1,m2); 
	cout << "The sum is:"; 
	sum.display_money();

	cout << m1.add(m1,m2);
	
	

	return 0; 
}

AltMoney::AltMoney()
{
}

AltMoney::AltMoney(int d, int c) 
{ 
	dollars = d; 
	cents = c; 
}

void AltMoney::display_money() 
{ 
	cout << "$" << dollars << ".";
	if(cents <= 9)
		cout << "0"; //to display a 0 in the left for numbers less than 10 
	cout << cents << endl; 
}

int AltMoney::add (AltMoney m1, AltMoney m2)
{
	int extra = 0; 
	sum.cents = m1.cents + m2.cents;
	if(sum.cents >=100)
	{
		sum.cents = sum.cents - 100;
		extra = 1;
	}
	sum.dollars = m1.dollars + m2.dollars + extra; 
}

void read_money(int& d, int& c) 
{
	cout << "Enter dollar \n";
	cin >> d; 
	cout << "Enter cents \n"; 
	cin >> c;
	if( d < 0 || c < 0)
	{
		cout << "Invalid dollars and cents, negative values\n"; 
		exit(1); 
	}
}
A friend function is defined as follows:
1
2
3
4
5
6
7
8
9
class MyClass
{
  int a;
  int b;
public:
  friend void SetAB(MyClass& mc);
};

void SetAB(MyClass& mc) { mc.a = 0; mc.b = 0; }


You don't have any friend functions in your code, you'd need the friend keyword for that. When I compile your code I get these problems:
src/soundsys.cpp:16:12: error: ‘get’ has not been declared
src/soundsys.cpp:16:15: error: expected ‘,’ or ‘...’ before ‘.’ token
src/soundsys.cpp: In function ‘int main()’:
src/soundsys.cpp:38:12: error: ‘add’ was not declared in this scope
src/soundsys.cpp: In member function ‘int AltMoney::add(AltMoney, AltMoney)’:
src/soundsys.cpp:70:2: error: ‘sum’ was not declared in this scope
src/soundsys.cpp:77:1: warning: no return statement in function returning non-void [-Wreturn-type]
make: *** [obj/soundsys.o] Error 1


First: int add (get.m1, get.m2); doesn't make sense because get, m1, and m2 are all not defined at this point. If add is your friend function then do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class AltMoney 
{
	public: 
		...
		friend AltMoney add (AltMoney& m1, AltMoney& m2);
	private: 
		...
};
...
AltMoney add (AltMoney& m1, AltMoney& m2)
{
	int extra = 0;
	AltMoney sum;
	sum.cents = m1.cents + m2.cents;
	if(sum.cents >=100)
	{
		sum.cents = sum.cents - 100;
		extra = 1;
	}
	sum.dollars = m1.dollars + m2.dollars + extra; 
	return sum;
}
Last edited on
Remove line 17 of your original post to clear up a great many errors:
int add (get.m1, get.m2);

To compile, resolve the remaining highlights:
http://pastie.org/8686857#37,41,69-70,72,75-76
Your errors/warnings were:
In function 'int main()':|
main.cpp|37|error: [b]'add' was not declared in this scope

main.cpp||In member function 'int AltMoney::add(AltMoney, AltMoney)':
main.cpp|69|error: 'sum' was not declared in this scope
main.cpp|77|warning: no return statement in function returning non-void [-Wreturn-type]

=== Build finished: 2 errors, 1 warnings (0 minutes, 0 seconds) ===


If you're confused about any particular line, explain your interpretations and the line number; I'm sure it will be answered.

Have fun.
Last edited on
Topic archived. No new replies allowed.