I have been trying to figure out for the life of me what I am doing wrong here, and I am coming up blank. My operator is returning the correct value in temp - as you can see highlighted in red, however, when I use the operator in main - it is not giving me the correct value. What is it that I am missing :-(

Implementation File

Term Term::operator +=(const Term &t)
{
  Term temp;
  if (exponent  == t.exponent)
    {
      temp.coefficient = coefficient + t.coefficient;
      temp.exponent = exponent;
    }

  cout<<"temp= "<<temp<<endl; //this is returning the correct value
  return (temp);
}

Here is the application file

void operatorTest(Term &x)
{
  Term y;
  cout<<"Enter another term: "<<endl;
  cin>>y;
  if (x != y)
    cout<<"The exponents are not equal. They cannot be added"<<endl;
  else
  x+=y;
  cout<<"The new term is: "<<x<<endl; //this is only giving me the value of x and not x+=y

}

Recommended Answers

All 6 Replies

I am not sure how to edit my post so, I will just do it this way - Is there any pertinent information I am missing in order for someone to help me achieve what I am trying to do? I have created a Term class which contains a coefficient and an exponent, and will only add them if the exponents match. I know I am maybe using the operator incorrectly or possibly referencing the Term class incorrectly when I am using the += operator - but I would really appreciate so much advice on what it is I am incorrectly doing. Sorry I am really new to C++ and just in the learning stages. :-)

Term& Term::operator +=(const Term &t)

Added the reference. Not positive because I don't know what the class Term consists of, but I don't think you can use cin with an object of a class. I am not completely sure of this, so it's worth looking into. Just throwing some suggestions out there.

Is Term a typedef or some kind of object, because if it is a class you can't input it simply like that.

operator+= is the in place addition operator. That is it alters the object it is called on, unlike the binary additional operator operator+ which does not alter the object it is called on at all but just returns a new result.

Your impletemtation is not in place, that is it does not alter the object it is called on,it creates a new temporary object temp and alters that.

They way you have written it x += y does not alter x which is what you are seeing. If you wrote z = (x += y) with your current implementation then you would find that x was unaltered by z had the sum of x and y.

You need to re-write your operator+= so that is does an in place calculation, that is so that the object that it is called on is altered to contain the new value. You will not need the variable temp at all in such an implementation.

I completely agreew with Banfa' explanation.. Something like this should do:

Term& Term::operator +=(const Term &t)
{
    if (exponent  == t.exponent)
    {
      coefficient +=  t.coefficient;
      //exponent = t.exponent; // it's basically a redundant statement as exponent = t.exponent as per the if condition
    }
	// Don't forget to add the else case !!!

  cout<<"val= "<<*this<<endl; //this will returning the correct value
  return *this;
}

I did actually ended up figuring this out in the wee hours of the morning - I solved it by this

Term& Term::operator +=(const Term &t)
{
    if (exponent  == t.exponent)
    {
      coefficient = coefficient + t.coefficient;
      exponent = exponent;

    }


  return *this;
}

Then when I use x+=y in the application program - x+=y then did in fact equal x=x+y. I appreciate all of you for your help! Thanks so much!!!

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.