tags:

views:

83

answers:

7

I have tried to overload the "+" operator for my class:

Vector operator+( const Vector& a, const Vector& b );

However,it tells me:

vector.h(12): error C2804: binary 'operator +' has too many parameters

I really don't get it. Please help me

+7  A: 

If you define the operator in the class, it should only receive 1 argument.

class Vector {
  ...
  Vector operator+ (const Vector& other) const {
    Vector res = *this;
    res += other;
    return res;
  }
  ...
};

The 2 argument version is used if you define it outside of the class definition.

class Vector {
 ...
};

Vector operator+ (const Vector& first, const Vector& second) {
  Vector res = first;
  res += second;
  return res;
}
KennyTM
For the second case, if instead of manually copying the first argument you use pass-by-value you provide the compiler a chance of optimizing away the copy if the first argument is a temporary. In `Vector operator+(Vector first, Vector const Vector foo(); ... a = foo() + b;` the compiler knows that the result of the `foo()` call is a temporary bound for destruction, so it can arrange the code so that it is built in place of the first argument to the `operator+` and elide the copy. With your proposed signature the compiler cannot avoid copying the object.
David Rodríguez - dribeas
+1  A: 

I guess you are placing the code inside the class, if that is the case, you only need to give 1 parameter (the other object), because by default the this object is on the left hand side.

phunehehe
+3  A: 

I believe you have declared this as member method of class Vector. In that case it should take only one parameter as the object on which this operator is called is implictly available through this pointer. Alternalitevly, you can declare the function as a friend of class Vector and take two parameters to it.

Naveen
A: 

This is because the Vector on the left of the operator and the one on the right are not the first and second arguments. The one before the + is actually this, which is automatically supplied as a Vector *. You only need one argument, and that's for the Vector to the right of the + operator.

This would be correct:

Vector operator+( const Vector& b );

You can access the left Vector by:

(* this)

... and the right Vector by:

b
Delan Azabani
A: 

When overloading + operator you can only have one parameter. The other parameter is "this"

nacho4d
A: 

Follow KennyTM, or else make your function as a friend..

like

friend Vector operator+( const Vector& a, const Vector& b );

An example can be found here..

liaK
A: 

The concrete error has been answered in a few answers already. Single parameter for the member method, two parameters for the free function version. But no hint into what path you should follow: should you remove the first parameter or else make it a free function?

The common idiom is that if you provide operator+ you might also want to provide operator+=, and if you are providing both, you can implement the first in terms of the latter. Implement operator+= as a member function (with full access to all the state) and operator+ as a free function that only needs to use the public operator+=:

class MyClass {
public:
   MyClass& operator+=( MyClass const & rhs );
};
MyClass operator+( MyClass lhs, MyClass const & rhs ) {
   return lhs+=rhs;
}

This way you have a single implementation for the operation, and you can provide both interfaces (a + b or a += b) so your interface is richer at almost no cost.

David Rodríguez - dribeas
What reason is there to create a free function? Wouldn't it usually be better OO to declare `operator+` as a class method also?
sje397
@sje397: The free function operator is symmetric with respect to the data types, while the member method cannot be. Consider a type `T` that can be implicitly converted from type `T2`, and two objects `t` and `t2` from those types. If `T` has a member method `operator+`, then `t + t2` is well formed and will call that operator, but `t2 + t` will be a compile time error. With the free function implementation, both operations are allowed and have the exact same meaning --which is something one would assume from addition...
David Rodríguez - dribeas
The compiler is not allowed to convert `t2` to `T` before calling a member method of `T`, but it is allowed to perform that conversion so to match the arguments of a function/method, as in `t + t2`.
David Rodríguez - dribeas
@sje397 It depends. Clean code implies short class definitions trying to follow the Single Responsibility Principle (One class should do few things, but do them well). If adding the definitions of algebraic operators to the class vector polutes the class definition because it makes it too long... then you should declare it as a non member function.
Stephane Rolland
@sje397 cf Chap 44. "Prefer writing nonmember nonfriendfunctions" in Bjarne Stroustrup's C++ In-Depths Series - C++ Coding Standards: 101 Rules, Guidelines, And Best Practices by Herb and Stutter
Stephane Rolland
The whole thing of having everything as a member method being more OO is arguable at the very least. But the discussion would be much longer than what can be handled in comments... but it is quite well handed in [What's in a class?](http://www.gotw.ca/publications/mill02.htm) by Herb Sutter
David Rodríguez - dribeas
Thanks David and Stephane - good explanations.
sje397