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

Time:10-01

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

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 implementation

#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 this happens? I've read a lot of papers and I've seen a lot of examples and all of them works but mine no.

I'm missing something?

CodePudding user response:

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;
  •  Tags:  
  • c
  • Related