Using Friends

I'm trying to learn using friends in classes, but I'm getting an error with my friend function

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
92
93
94
95
#ifndef MONEY_H
#define MONEY_H
using namespace std;
#include <iostream>
#include <iomanip>



class Money
{
private:
   int dollars;
   int cents;

public:

   Money()
   {
      setDollars(0);
      setCents(0);
   }

   Money(int dollars, int cents)
   {
      setDollars(dollars);
      setCents(cents);
   }


   int getDollars()  { return dollars; }
   int getCents()  { return cents; }
   
   void setDollars(int dollars) { this->dollars = dollars; }
   void setCents(int cents) { this->cents = cents; }

 
void prompt() 
{
   int dollars;
   int cents;

   cout << "Dollars: ";
   cin >> dollars;

   cout << "Cents: ";
   cin >> cents;

   setDollars(dollars);
   setCents(cents);
}

// Here is my problem
   friend void display(Money &money); 

};

void display(Money &money) 
{
   cout << "$" << money.dollars << ".";
   cout << setfill('0') << setw(2) << money.cents;
}


#endif

// This is my main:

#include <iostream>
using namespace std;

#include "money.h"

/****************************************************************
 * Function: main
 * Purpose: Test the money class and practice operators
 ****************************************************************/
int main()
{
   Money account1;
   Money account2;

   // Get the input from the user
   account1.prompt();
   account2.prompt();
   cout << endl;

   display(account1);
   cout << endl;
   display(account2);

   return 0;
}




I am getting this error " In function `display(Money&)':
check12a.cpp:(.text+0x0): multiple definition of `display(Money&)'
money.o:money.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status"

Does anyone know what I'm doing wrong??

Thanks

Here is my makefile

1
2
3
4
5
6
7
8
9
10
11
12

a.out : money.o check12a.o
	g++ money.o check12a.o

money.o : money.h money.cpp
	g++ -c money.cpp

check12a.o : money.h check12a.cpp
	g++ -c check12a.cpp

clean :
	rm *.o *.out
Last edited on
What's your money.cpp file ?

The problem here is that you are including money.h in both your main.cpp file and money.cpp file, and in money.h you are defining the display function. Thus both main and money end up defining the display function which confuses the linker.
I hope you understand this concept.

To fix this, move this block
1
2
3
4
5
void display(Money &money) 
{
   cout << "$" << money.dollars << ".";
   cout << setfill('0') << setw(2) << money.cents;
}

from money.h to money.cpp
This is not helping.


If declaring on my money.cpp as void Money::display(Money &money) I get this error

money.cpp:31: error: no ‘void Money::display(Money&)’ member function declared in class ‘Money’
make: *** [money.o] Error 1

but declaring just as void display(Money & money) I get this error:

money.cpp: In function ‘void display(Money&)’:
money.cpp:31: error: redefinition of ‘void display(Money&)’
money.h:59: error: ‘void display(Money&)’ previously defined here
money.h:18: error: ‘int Money::dollars’ is private
money.cpp:33: error: within this context
money.h:19: error: ‘int Money::cents’ is private
money.cpp:34: error: within this context
If I run this program here, but commenting line 71, the program works as it should
display isn't a member function of class Money.
This is what needs to go in money.cpp

1
2
3
4
5
void display(Money &money) 
{
   cout << "$" << money.dollars << ".";
   cout << setfill('0') << setw(2) << money.cents;
}

That's just it.
> If I run this program here, but commenting line 71, the program works as it should

Yes, that's what I was saying, your initial error was due to display being defined in two files, if you remove one instance the linking goes fine
Perfect!!!!

Thanks =]
Topic archived. No new replies allowed.