I have a class called Sensors that has a variable called 'value' which is a float. I want to be able to re-write the following:
averageTemperature = averageTemperature + sensors[count].value;// sensors is an array of Sensors object
to
averageTemperature = averageTemperature + sensors[count];
//averageTemperature is also a float.
So far I have done this:
class Sensors
{
public:
// alot of other code here
float getValue()const { return value; }
|
friend float operator + (float fl1,const Sensors& sensor1);// float+class=float |
|
protected:
float value{};
};
float operator+(float fl1, const Sensors& sensor1) |
{return fl1+sensor1.getValue();} |
As you can see there is a problem i.e a friend function has to be used. Is there anyway around this.(I have no idea why the formatting has gone wrong!)