What I am trying to do here is to overload the % operator so it multiplies the numerator by an given number. All of my other overloaded operators in the program work perfectly, but this one is giving me problems. To isolate this issue, I created a separate smaller program with only this overloaded operator.
From the modu.h file (some hopefully irrelevant parts omitted):
class fraction {
private:
int numerator, denominator;
public:
fraction ();
fraction (int n, int d);
void print ();
void operator% (int x);
};
// the constructors and print functions work fine for the other overloaded
// operators so I decided to omit them
void fraction::operator%(int x) {
numerator = numerator * x;
}
When I use them in the main.cpp file like this
fraction frct (2, 3); // declare a fraction with a value of 2/3
frct = frct % 3;
I get these errors
modu.cpp:9:17: error: no match for ‘operator=’ in ‘frct = frct.fraction::operator%(3)’
modu.cpp:9:17: note: candidate is:
In file included from modu.cpp:3:0:
modu.h:6:7: note: fraction& fraction::operator=(const fraction&)
modu.h:6:7: note: no known conversion for argument 1 from ‘void’ to ‘const fraction&’
And when I use it like " frct = frct.%(3); ", I get the error:
modu.cpp:9:15: error: expected unqualified-id before ‘%’ token
I have checked over this several times for missing semicolons and curled brackets, but everything seems like it should be in order, and the operator% function does not look any different from my working overloaded operators.
operator*?