Issues with Operator += in C++

I just went back from operators' declaration in C# and I have learned that, in general, if I have a + and a = operators, I implicitly get a += operator (and the same goes for -, *, /). So I opened one of my old C++ projects and checked if I had the same results. I tried doing this in a "Vector2D" class after deleting the += operator:

Vector2D a(2, 3);
Vector2D b(50, 14);

a += b;

The compiler says "no operator "+=" matches these operands". Why? Here is the code of + and = :

Vector2D Vector2D::operator+(const Vector2D& v) const
{
    return Vector2D(x + v.x, y + v.y);
}

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

EDIT

This works btw:

a = a + b;


from Recent Questions - Stack Overflow https://ift.tt/36XhXbq
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)