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!

1
  • 1
    You have a dangling reference. Anyway, did you ever declare the overload where main.cpp can see it before using it? Commented Mar 6, 2014 at 22:34

3 Answers 3

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){
 // ...
Sign up to request clarification or add additional context in comments.

1 Comment

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
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);

Comments

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() );
}

4 Comments

@suitegamer Read my post because it shows how correctly to define the operator.
I'd say no const on the return type and wrapping it around operator+= is more common and idiomatic.
@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
But it also prevents moving the return value out (although I will agree when you say that RVO will happen in all likelihood).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.