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

//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
}

There are several issues with this, but the biggest is that your class doesn't have an assignment operator defined. As a result, the compiler doesn't know how to assign the new value of c (after adding d to it) to x.

Also, you may want to find an arithmetic textbook and review how to add fractions properly. This algorithm isn't even close.

There are several issues with this, but the biggest is that your class doesn't have an assignment operator defined. As a result, the compiler doesn't know how to assign the new value of c (after adding d to it) to x.

Also, you may want to find an arithmetic textbook and review how to add fractions properly. This algorithm isn't even close.

Thanks Fbody,

Yeah, I know about the calculation problem (I need to find the LCD first). Haven't gotten around to fixing that yet. But you mention that "but the biggest (problem) is that your class doesn't have an assignment operator defined". I',m not sure I understand what that means or how it would be implemented. Can you please offer clarification?

Many Thanks!

Tyster

Observe:

const int setSize(15);

int someInt;

someInt = setSize;

In this little snippet, Line 5 is an assignment statement. For an assignment statement to function properly, the assignment operator (" operator = ") must be defined for the dataType. This snippet works because the built-in dataType int has the assignment operator already defined for it.

In your particular case, you are trying to assign the updated value of Rational c to Rational x. However, the system doesn't know how to perform this assignment because you haven't told it how to do it.

Read this.

Observe:

const int setSize(15);

int someInt;

someInt = setSize;

In this little snippet, Line 5 is an assignment statement. For an assignment statement to function properly, the assignment operator (" operator = ") must be defined for the dataType. This snippet works because the built-in dataType int has the assignment operator already defined for it.

In your particular case, you are trying to assign the updated value of Rational c to Rational x. However, the system doesn't know how to perform this assignment because you haven't told it how to do it.

Read this.

Thx, Fbody. I see what you're saying and I'll research this more to see if I can figure out how to do that. Many Thanks!

Tyster

The way you set up your addition function, it will add the value of the parameter to "this" object, so you don't need the assignment at all, or you need to redefine the implementation. This line:

x = c.addition(d);

actually adds d to c and stores the result in c, like if you did "c += d;". So the result of the operation is then stored in c and that's it. The reason why you get this compilation error (not an exception btw) is that your function addition does not return anything (void return type). It is not possible to assign nothing to something. If you want your addition function to behave like "x = c + d;", you would need something like this:

//In Rational.h

class Rational {
  public:
    Rational(int num = 1, int denom = 2); //Default constructor
    Rational addition(Rational); //notice the return type Rational.
    //other functions

  private:
    int numerator;
    int denominator;
};

//In Rational.cpp, constructor

Rational::Rational(int x, int y)
{
	numerator = x;
	denominator = y;
}

Rational Rational::addition(Rational f)
{
   //Add numerators and denominators
   return Rational(numerator + f.GetNumerator(), denominator + f.GetDenominator());
}

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

  return 0;
};

As Fbody said, your addition function is of course not correct, but this should get you going.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.