3

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    You're calling `+=` on pointers instead of the objects they point to. – interjay Sep 30 '21 at 15:52
  • A `Vector2D*` ≠ `Vector2D`. (Why would they be pointer member variables in the MovingObject in the first place?) – Eljay Sep 30 '21 at 15:58
  • 4
    `~Vector2D(){}` -- Off-topic, but do not write empty destructors with no implementation. Either remove it, or declare it as `=default`. I know you may have seen other code samples that may do this empty destructor stuff, but this is deemed a code smell since C++11. – PaulMcKenzie Sep 30 '21 at 15:59
  • 4
    This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_VECTOR2D_H_`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Sep 30 '21 at 15:59
  • 2
    Just to show you the implications of that empty destructor, [look at this program](http://coliru.stacked-crooked.com/a/83c14889440d4a33). Just the change of removing the empty destructor changes the traits of the class. That may be a crucial thing when it comes to the compiler (or other templated code) working with the class. – PaulMcKenzie Sep 30 '21 at 16:08
  • 1
    Why are you using `Vector2D` pointers? Is there polymorphism involved or are they pointing at objects owned "elsewhere"? – Ted Lyngmo Sep 30 '21 at 16:09
  • @PaulMcKenzie oh thanks! Yes the book I'm reading use some of them empty. Ok I'll change that. – Cosimo Davide Viggiano Sep 30 '21 at 16:11
  • 1
    @TedLyngmo yeah I'll have to change they. – Cosimo Davide Viggiano Sep 30 '21 at 16:14
  • 1
    @CosimoDavideViggiano The books are probably old -- but to their defense, a lot of C++ programmers get into this (bad) habit and just write empty destructors, almost by rote. Worse yet, many automatic C++ class/code generators that the programmer has no control over produce destructors that are empty in the classes they generate. In any event, write a destructor when there is something that you actually must do on object destruction. – PaulMcKenzie Sep 30 '21 at 16:15
  • 1
    A generalization of Paul McKenzie's point is covered in [the Rule of Zero](https://en.cppreference.com/w/cpp/language/rule_of_three). Make sure you are familiar with the Rules of Three and Five as well. It's hard to write effective and efficient C++ without understanding Three/Five/Zero. – user4581301 Sep 30 '21 at 17:01
  • 1
    More information about Pete Becker's point: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). Misusing an underscore doesn't bite that often, but when it does, things get weird. – user4581301 Sep 30 '21 at 17:01

1 Answers1

3

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;
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    Damn! That worked like a charm! Shame on me, I forgot this!! Thanks to everyone for the comments and helping me to improve. And you are right, I'll change from pointers to reference, no need to be pointers at least not these. – Cosimo Davide Viggiano Sep 30 '21 at 16:10