### 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...

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` ``````// P2_4a.cpp - This program adds money of two different people #include #include 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:
 ``123456789`` ``````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:
 ``12345678910111213141516171819202122`` ``````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
closed account (j3Rz8vqX)
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