0

I am trying to overload the += opperator on my template Vector class.

template<unsigned int dimensions, typename TValue>
class Vector
{
private:
    std::array<TValue, dimensions> m_values;
public:
    Vector(){
        for (int i = 0; i < dimensions; i++){
            m_values[i] = TValue();
        }
    };
    Vector(std::array<TValue, dimensions> elements){
        for (int i = 0; i < dimensions; i++){
            m_values[i] = elements[i];
        }
    };
    
    inline void set(VectorDimensions dimension, TValue value){
        m_values[dimension] = value;
    };
    
    inline TValue get(VectorDimensions dimension) const{
        return m_values[dimension];
    };

    inline unsigned int getSize() const{
        return dimensions;
    };

    const std::array<TValue, dimensions> getValues() const{
        return m_values;
    };

    friend ostream& operator<<(ostream& os, const Vector<dimensions, TValue>& vt) {
        array<TValue, dimensions> values = vt.getValues();
        os << '[';
        for (unsigned int i = 0; i < vt.getSize(); i++){
            os << values[i] << values[i+1] ? ", " : "";
        }
        os << ']';
        return os;
    };

    friend Vector<dimensions, TValue>& operator+=(const Vector<dimensions, TValue>& vt) {
        array<TValue, dimensions> values = vt.getValues();
        for (unsigned int i = 0; i < vt.getSize(); i++){
            m_values[i] += values[i];
        }
        return *this;
    };

};

Upon adding the overload for the += opperator I get many of the following errors:

error C2805: binary 'operator +=' has too few parameters

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2334: unexpected token(s) preceding '{'; skipping apparent function body.

error C2238: unexpected token(s) preceding ';'

syntax error : missing ';' before '<'

error C2143: syntax error : missing ';' before '++'

error C2143: syntax error : missing ')' before ';'

error C2059: syntax error : 'return'

error C2059: syntax error : 'for'

error C2059: syntax error : ')'

An explanation as to why or how these errors actually get caused by whatever it is that I have done wrong may be useful. Thanks

Community
  • 1
  • 1
Edward J Brown
  • 333
  • 1
  • 5
  • 17
  • Well, the main mistake is that there is that `+=` actually acts as operators `+` and `=` separately. `a += b` is the same as `a = a + b` so you should implement `operator+` and, if necessary, `operator=`. – Zach P Jun 28 '15 at 16:15
  • 2
    `operator+=` must be implemented either as a member function taking one parameter, or a non-member taking two parameters. You attempt to implement it as a non-member taking one paramerer: hence "too few parameters" error. Drop `friend` in front. – Igor Tandetnik Jun 28 '15 at 16:19
  • 1
    @ZachP: Quite the contrary, I'd say (if I understand you correctly). `operator+` should be implemented in terms of `operator+=`. – Christian Hackl Jun 28 '15 at 16:19
  • @ChristianHackl There is no point of implementing `operator +=`. Why not implement `operator+` and `operator=` instead? What about this line: `a = a + b + c + d`, or even `a += b + c + d` when all are of the same class. I see no point in that. Note that the error is obviously not here, I just talked about the logic behind the code. – Zach P Jun 28 '15 at 16:25
  • @ZachP: It's to avoid code duplication. `+=` is the basic building block, and `+` uses it (along with `=`) in the form of `T result = a; result += other;`. – Christian Hackl Jun 28 '15 at 16:33
  • @ChristianHackl Read [here](http://stackoverflow.com/questions/28343969/operator-overloading-operator-vs-operator) if you want. It's not to avoid code duplication, the only thing that it produces (if you wrote your code efficiently) is less or no memory duplication whatsoever. – Zach P Jun 28 '15 at 17:31
  • @ZachP: It is also about code duplication. `+` and `+=` obviously do similar things. Your link completely supports the idea of implementing `+` in terms of `+=`, see e.g. Mike Seymour's answer. – Christian Hackl Jun 28 '15 at 17:43

1 Answers1

0

I was able to stop the errors by removing the friend keyword from before the += operator definition. According to this page http://en.cppreference.com/w/cpp/language/operators

class X {
 public:
  X& operator+=(const X& rhs) // compound assignment (does not need to be a member,
  {                           // but often is, to modify the private members)
    /* addition of rhs to *this takes place here */
    return *this; // return the result by reference
  }

  // friends defined inside class body are inline and are hidden from non-ADL lookup
  friend X operator+(X lhs,       // passing first arg by value helps optimize chained a+b+c
                    const X& rhs) // alternatively, both parameters may be const references.
  {
     return lhs += rhs; // reuse compound assignment and return the result by value
  }
};

This is how the + and += opperators should be overloaded.

Edward J Brown
  • 333
  • 1
  • 5
  • 17
  • `operator<<` must be implemented as a non-member function; `friend` makes it so. `operator+=` could be implemented as a non-member - but then it should take two parameters. Yours takes one, which would only work if it's a member function. Dropping `friend` made it so. – Igor Tandetnik Jun 28 '15 at 16:22
  • Thanks, It sounds stupid now but I hadn't released the true significance of how a function cannot be a friend and a class member. – Edward J Brown Jun 28 '15 at 16:32
  • Well, you can have a member function of one class be a friend of another. But that's not what your code is doing. – Igor Tandetnik Jun 28 '15 at 16:33