0

I'm just trying to create an overloaded friend function (Am I phrasing that right?) to add two different types of the class 'coins'. It seems I cannot get the syntax right. Can someone point me in the right direction?

The logic implemented in my functions.cpp file:

coins operator+(const coins  &num1, const coins &num2)
{
    int dollars = num1.dollars + num2.dollars;
    int cents = num1.cents + num2.cents;
    return coins(dollars, cents);
}

My functions.h declaration of the friend function:

coins operator+(const coins  &num1, const coins &num2);

My class file declaring a friend in coins.h:

friend coins operator+(coins); 

Can anyone point me in the right direction? Should I post the complete code?

ilim
  • 4,477
  • 7
  • 27
  • 46

2 Answers2

1

Inside the coins class you should put:

friend coins operator+(const coins&, const coins&);

(i.e. it has a left-hand-side operand and a right-hand-side operand).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • ah. should have seen that. It still tells me however, that "In file included from pa3.cpp:6:0: pa3functions.h:17:2: error: ‘coins’ does not name a type In file included from pa3functions.cpp:4:0: pa3functions.h:17:2: error: ‘coins’ does not name a type pa3functions.cpp:17:1: error: ‘coins’ does not name a type " –  Feb 27 '13 at 04:33
  • @Zane: Sounds like the pa3functions.cpp is not aware of `coins`. – ChiefTwoPencils Feb 27 '13 at 04:44
  • Sorry to come back, but am I to create another constructor for this? Now im getting the error pa3functions.cpp: In function ‘coins operator+(const coins&, const coins&)’: pa3functions.cpp:21:32: error: no matching function for call to ‘coins::coins(int&, int&)’ –  Feb 27 '13 at 04:51
  • Yes... given the implementation of `operator+` in your question, you do need to have a constructor `coins(int dollars, int cents)`.... – Tony Delroy Feb 27 '13 at 05:23
1

The error message X does not name a type seems to indicate that the compiler does not recognize X as being a type, either because it is not seeing the definition or because there is something else that is found by lookup before it gets to the type.

Check that you are including the appropriate header, and that where you are defining the operator there is no other symbol coins in scope hiding the type. Also beware that a operators should be defined in the same namespace that the types that they operate on.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Thank you David, I actually just figured that out a second before you posted. I was now just wondering if i needed to create a class constructor for the friend function. i keep getting the new error pa3functions.cpp: In function ‘coins operator+(const coins&, const coins&)’: pa3functions.cpp:21:32: error: no matching function for call to ‘coins::coins(int&, int& –  Feb 27 '13 at 05:02
  • @Zane: It is impossible to say without seeing the code. That error looks like the implementation of `operator+` is doing something like `return coins(lhs.value,rhs.value);` (or something alike... basically trying to create a new `coins` object from two `int` lvalues) – David Rodríguez - dribeas Feb 27 '13 at 14:05