0

SOLVED

I want to overload the binary addition operator (operator+). My compiler (Borland Builder) says that there is a declaration syntax error on operator overload definition.

Here is my code:

class Foo{
 public:
  double x,y,z;
  Foo();
  Foo(double xi, double yi, double zi) : x(xi), y(yi), z(zi) {};

  friend Foo operator+(const Foo &a, const Foo &b);
} // Here is the error - no closing ';'

Foo operator+(const Foo &a, const Foo &b) //Error E2141 Declaration syntax error
{
 return Foo(a.x + b.x, a.y + b.y, a.z + b.z);
}

I did same as here. Why do I get the compiler error?

EXAMPLE, SEEK FOR THE QUESTION IN COMMENTS TO THE ANSWER

Foo& operator*(const double &b, Foo& foo){
    foo.x*=t;
    foo.y*=t;
    foo.z*=t;
    return foo;
}
.
int main(){
    Foo temp;
    Foo bar = 4*temp;
    return 0;
}

1 Answers1

-1
  1. Well, there's missing ; after class declaration.
  2. There's no need for friend (even if friend then friend:)
  3. You can declare operator+ as a class member so you will have access to private members as well.

Sample:

#include  <iostream>

class Foo{
    public:
        double x,y,z;
        Foo();
        Foo(double xi, double yi, double zi) : x(xi), y(yi), z(zi) {};

        Foo operator + (const Foo &a) const;
};

Foo Foo::operator + (const Foo &a) const {
        return Foo(a.x + this->x, a.y + this->y, a.z + this->z);
}

int main() {
    Foo f1(1.0, 1.1, 1.2);
    Foo f2(1.6, 1.1, 1.2);
    Foo f3 = f1 + f2;

    std::cout << "f3.x=" << f3.x << std::endl;
    return 0;
}
Vicctor
  • 803
  • 6
  • 13
  • Thank you very much for your answer! Should I delete this question becouse of uselessness? – Constructovec Mar 31 '18 at 20:02
  • Be careful though: declaring the operator as a member will prevent commutativity, which you might want. This is the reason most people declare the operator as a free function. See [this](https://stackoverflow.com/questions/4622330/operator-overloading-member-function-vs-non-member-function) for more information. – BobMorane Mar 31 '18 at 20:46
  • Thanks for your comment, @BobMorane! I want to use this class as a 3D vector. I'm not sure if commutativity is necessary here, except situation of multiplying vector on number. Am I right? – Constructovec Mar 31 '18 at 20:58
  • In case of implementing 3d vector, you can simply consider for searching for something ready made like this: https://r3dux.org/2012/12/vec3-a-simple-vector-class-in-c/ – Vicctor Mar 31 '18 at 21:18
  • It's better to use non-member overload for `operator+`. [See here](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/4421729#4421729). The only problem in the original code was missing semicolon. – M.M Mar 31 '18 at 21:50
  • Thanks for you replies! Could you tell me, is it okay to always return from overloaded operator refererence to an object, to use it in copy constructors? I have add an example to main post. – Constructovec Apr 01 '18 at 17:43
  • @Constructovec A vector space has as one of its axioms commutativity. So I would say it is necessary, otherwise you will have something less than a vector. For this: "is it okay to always return from overloaded operator refererence to an object, to use it in copy constructors?", I would suggest writing another question, with example code showing clearly showing the issue you are referring to. – BobMorane Apr 01 '18 at 22:17
  • @BobMorane Created :) https://stackoverflow.com/questions/49603441/overloaded-operator-as-argument-of-copy-constructor – Constructovec Apr 01 '18 at 23:09