-
Overriding = operator.
I am working on a project using templates. I have a template class called Measures and another regular class called Meters.
I overrode the + operator in Measures and now want to use that
function when I add two Meters together. However, all I get is an error saying error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Meters' (or there is no acceptable conversion).
I can't figure out what I'm doing wrong.
Here's the code for the = operator, the + operator, and main.
// Overload = operator.
template <class T>
Measure<T>& Measure<T>::operator=(const Measure &rhs)
{
if (this == &rhs)
{
return *this;
}
this.measure = rhs.getMeasure();
return *this;
}
// Overload + operator.
template <class T>
T& Measure<T>::operator+(Measure &s1)
{
double v1, v3;
v1 = s1.getMeasure();
v3 = v1 + s1.measure;
Measure s3(v3);
return s3;
}
int main()
{
double x = 2.0;
int y = 3;
Meters m1(5.0);
Measure<Meters> theMeasure(4.0);
Measure<Meters> anotherMeasure(3.0);
Measure<Meters> finalMeasure(0.0);
finalMeasure = theMeasure + anotherMeasure;
cout << finalMeasure.getMeasure();
return 0;
}:(
-
Have you overloaded the = operator in the Meters class?
-
I don't know how, but I figured it out. Its working now.
Thanks anyway.
-
-
Do not return a reference where you must return an object....
operator + should return an object and not a reference. At present you are returning a reference to an object that has gone out of scope.....