Search code examples
c++operator-keywordfriend

error when creating friend function, "coins does not name a type"


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?


Solution

  • 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).