user1825608
user1825608

Reputation:

Overloading + correctly in c++

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

Upvotes: 0

Views: 121

Answers (4)

juanchopanza
juanchopanza

Reputation: 227390

Since you declared it as a friend, your addition operator is not a member function, hence it must take two parameters, and has no access to this:

friend Linia operator+ (const Linia& lhs, const Linia& rhs)
{
  return Linia(lhs.a, lhs.b, lhs.c + rhs/lhs.b);
}

This follows the logic in your code but will only work if there is an operator double operator/(const Linia& lhs, double). Something that intuitively would make sense would be

friend Linia operator+ (const Linia& lhs, const Linia& rhs)
{
  return Linia(lhs.a+rhs.a, lhs.b+rhs.b, lhs.c + rhs.c);
}

Finally, you could avoid the friend declaration completely and just declare the operator as a standard non-member function. It doesn't access any private or protected data, so it doesn't need to be a friend.

Upvotes: 1

Matt Kline
Matt Kline

Reputation: 10477

You have some basic syntax issues.

  • this is a pointer so you need to use -> to dereference it.

  • I assume you mean this->c + i.c instead of this->c + i

  • You 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 const references (since you shouldn't be modifying the operands).

I think you mean to have something like this:

class Linia{
public:
    double a,b,c;
    Linia (double a, double b, double c){
        this->a = a;
        this->b = b;
        this->c = c;
    }
    Linia operator+ (const Linia& i){
        return Linia(this->a, this->b, this->c + i.c / this->b);
    }
};

which you can clean up to be something like this:

class Linia{
public:
    double a,b,c;
    Linia (double a, double b, double c)
        : a(a), b(b), c(c)
    { }

    Linia operator+ (const Linia& i){
        return Linia(a, b, c + i.c / b);
    }
};

Upvotes: 5

Mr.C64
Mr.C64

Reputation: 42924

Assuming that your operator+ is a binary operator that takes two Linias as input and returns a Linea having added coefficients, you may want to define a free function operator+ overload, with proper const correctness of the input Linias, like this:

Linia operator+(const Linia& lhs, const Linia& rhs)
{
    return Linia
    (
        lhs.a + rhs.a,
        lhs.b + rhs.b,
        lhs.c + rhs.c
    );
}

Upvotes: 1

James Kanze
James Kanze

Reputation: 153909

There are at least two problems in your code. First, you've declared operator+ to be a friend, so it is a free function, with no this. Regretfully, from the code, I'm not able to figure out what you're trying to do (overload unary + or overload binary +), so it's difficult to say more. Second, you're trying to return the results of new, which is a pointer, not an object type. operator+ should always return an object type, so you don't want the new.

Upvotes: 1

Related Questions