0

I'm just wondering if someone could point out an issue that i'm having.

I have three files: main.cpp / Fraction.cpp / Fraction.h

Here is the relevant Fraction.h (class) information:

class Fraction{


//Variables

    int numerator;
    int denominator;

};

Inside the main.cpp, I declare some objects:

Fraction left, right, result, ref;

And then try to add them together:

result = left + right;

So then inside my Fraction.cpp file (I know its not where its supposed to be) is my operator overloading function:

Fraction& operator+(const Fraction& l, const Fraction& r){

 Fraction sum;

 int numSum, denSum;

 numSum = l.numerator2() + r.numerator2(); // these functions get the values

 denSum = l.denominator2() + r.denominator2(); // these functions get the values

 sum.set(numSum, denSum);

 return sum;

}

The following error occurs telling me that no function matches the expression in the main:

w6.cpp:29:20: error: invalid operands to binary expression ('Fraction' and 'Fraction')
 result = left + right;

Does anyone have any ideas as to why this is occurring? Thank you!

suitegamer
  • 421
  • 2
  • 7
  • 15
  • 1
    You have a [dangling reference](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope). Anyway, did you ever *declare* the overload where `main.cpp` can see it before using it? – chris Mar 06 '14 at 22:34

3 Answers3

1

You have to declare the overload in the header and define it in the cpp file. Ex.:

MyHeader.h:

Fraction& operator+(const Fraction& l, const Fraction& r);

MyFile.cpp:

Fraction& operator+(const Fraction& l, const Fraction& r){
 // ...
kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
  • Oh my gosh I had it in there however I guess it didn't save properly. Sublime text was trying to save it to an area on my HDD where I don't have write permissions. Wow. Thank you very much! :P – suitegamer Mar 06 '14 at 22:39
0

I think the compiler doesn't know you overloaded the + operator. You need to declare it in your (Fraction.h) header file:

Fraction& operator+(const Fraction& l, const Fraction& r);
Aliou
  • 1,065
  • 11
  • 17
0

It seems there are two problems. File with main does see the declaration of the operator. You have to include the declaration in the header. The second problem is that the operator is defined incorrectly. It has undefined behaviour because you return reference to a local object.

Change it the following way

const Fraction operator+(const Fraction& l, const Fraction& r){

   return Fraction(  l.numerator2() + r.numerator2(), l.denominator2() + r.denominator2() );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @suitegamer Read my post because it shows how correctly to define the operator. – Vlad from Moscow Mar 06 '14 at 22:46
  • I'd say no const on the return type and wrapping it around `operator+=` is more common and idiomatic. – chris Mar 06 '14 at 22:47
  • @chris In fact this class simulates arithmetic types. So I prefer to use const that there will not be a posiibility to assign a value to the expression – Vlad from Moscow Mar 06 '14 at 22:49
  • But it also prevents moving the return value out (although I will agree when you say that RVO will happen in all likelihood). – chris Mar 06 '14 at 22:51