Why I'm getting error: invalid operands to binary expression after overloaded += operand?

Viewed 191

I'm trying to overload the += operator for a simple mathematical Vector class, to sum the elements of two vectors, like this:

vector1 += vector2

Part of Vector2D.h:

#ifndef _VECTOR2D_H_
#define _VECTOR2D_H_

class Vector2D
{
public:

    Vector2D():x(0),y(0){}
    ~Vector2D(){}

    /* All the mathematical operation ... */

    // Overloading operators
    Vector2D& operator+=(const Vector2D& rhs);
    Vector2D& operator*=(const Vector2D& rhs);
    Vector2D& operator/=(const Vector2D& rhs);
    Vector2D& operator-=(const Vector2D& rhs);

private:

    double x;
    double y;
};

Then part of Vector2D.cpp:

#include "Vector2D.h"

Vector2D& Vector2D::operator+=(const Vector2D& rhs)
{
    x += rhs.x;
    y += rhs.y;
    return *this;
}

Now, in the .cpp class, I want to use the operator:

void MovingObject::move()
{
    m_pVelocity += m_pAcceleration;
    m_pVelocity->limit(m_fMax_speed);
    m_pPosition += m_pVelocity();
    m_pAcceleration->zero();
}

The variables m_pVelocity, m_pAcceleration and m_pPosition are all Vector2D* pointers.

When I try to compile, this is what I get from the compiler:

compiler's result

Why does this happen? I've read a lot of papers, and I've seen a lot of examples, and all of them work, but mine does not.

Am I missing something?

1 Answers

It looks like on this line you have two pointers

m_pVelocity += m_pAcceleration;

so you'd need to dereference them to use this operator

*m_pVelocity += *m_pAcceleration;
Related