I need to overload the * operator in c++. So I have made a class called Element which must overload this opperator to work on a double value stored within it. This is what I have done in the implementation file:
#include "Element.h"
#include <iostream>
using namespace std;
// Other code that is not relevant
Element Element::operator * ( const Element &obj)
{
d *= obj.d;
return *this;
}
This doesn’t work. It throws an error saying: “no match for ‘operator*’ in ‘8 * c’
In the main file I have:
d = a = 8 * c - 4 + b;
where d, a, c and b are all objects of class Element
You really have to understand what you are doing here, you are overloading the ‘*’ operator for the Element class, but you’re doing so while expecting another element has a ‘parameter’.
The code you wrote is actually expecting this kind of code
As mentionned, you might want to take a look at : http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/