I have created class which represents line aX + bY = c and I wanted overload + operator to higher the line(return higher Line so I have done that below but compilator says invalid use of this
class Linia{
public:
double a,b,c;
Linia (double a, double b, double c){
this->a = a;
this->b = b;
this->c = c;
}
friend Linia operator+ (double i){
return new Linia(a, this->b, this->c + i/this->b);
}
};
I would like to return new Linia object which is has fields like shown above i is int i do not want to modify original object
You have some basic syntax issues.
thisis a pointer so you need to use->to dereference it.I assume you mean
this->c + i.cinstead ofthis->c + iYou don’t need to (and probably shouldn’t) have the operator be a friend.
Operators that return a new instance (like
operator+) should return by value, not allocate on the heap.Operators generally take parameters as
constreferences (since you shouldn’t be modifying the operands).I think you mean to have something like this:
which you can clean up to be something like this: