0
votes

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

1
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
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
@ZachP: Quite the contrary, I'd say (if I understand you correctly). operator+ should be implemented in terms of operator+=. - Christian Hackl
@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
@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

1 Answers

0
votes

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.