Hi folks,
I'm quite new to C++ and I'm getting an exception I don't understand.
I have a class named 'Rational' that performs calculations on fractions. It has a default constructor as follows...
//In Rational.h
public:
Rational(int num = 1, int denom = 2); //Default constructor
void addition(Rational);
void subtraction(Rational);
void multiplication(Rational);
void division(Rational);
//other functions
private:
int numerator;
int denominator;
//In Rational.cpp, constructor
Rational::Rational(int x, int y)
{
numerator = x;
denominator = y;
}
The problem I am having is when I call the addition function in main(). First I create 3 Rational objects and then I call the addition function, as follows...
int main()
{
Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects
x = c.addition( d ); // adds object c and d; sets the value to x
...
The addition function in Rational.cpp is:
//Add 2 Rational numbers
void Rational::addition(Rational f)
{
//Add numerators and denominators
numerator += f.GetNumerator(); //accessor defined elsewhere in this class
denominator += f.GetDenominator(); //GetNumerator defined elsewhere in this class
}
When the addition function is called I get the following error message from gcc...
"16 E:\CSC_134_Cpp\Lab5\Rational_Number\Rational_Number\Rational_Driver.cpp no match for 'operator=' in 'x = (&c)->Rational::addition(Rational(((const Rational&)((const Rational*)(&d)))))' "
It looks like it thinks the object I pass in the addition function ('d') should be a const or a reference but I tried that and it didn't work (but maybe I did it wrong). I admit the error itself seems a little baffling. In any event, it certainly seems like a constructor problem. Can anyone please offer any advice about this? Many Thanks for helping out a newbie!
Tyster