0

So I just started out on trying some things in C++, basically I want to define some 2D vector functions. I want to overload the multiplication operator in such a way that it can be used to do a dot product, as well as a scalar multiplication, depending on the operands given. I somehow get the error "Cannot overload functions distinguished by return type alone", eventhough I clearly require the arguments of the functions to be different. I distributed my code to a header and a c++ file. The header-file shows the error by itself, so here it is:

#pragma once

class Vector2D {

public:
    float x;
    float y;

    Vector2D();
    Vector2D(float x, float y);

    Vector2D& Add(const Vector2D& vec);
    Vector2D& Subtract(const Vector2D& vec);
    Vector2D& ScalarMultiply(const float scalar);

    float Dotproduct(const Vector2D& vec);

    friend Vector2D& operator+(Vector2D& v1, const Vector2D& v2);
    friend Vector2D& operator-(Vector2D& v1, const Vector2D& v2);
    friend Vector2D& operator*(Vector2D& v1, const float scalar);   //This line causes trouble

    friend float operator*(Vector2D& v1, const Vector2D& v2);

    Vector2D& operator+=(const Vector2D& vec);
    Vector2D& operator-=(const Vector2D& vec);
    Vector2D& operator*=(const float scalar);

    float operator*=(const Vector2D& vec);

};

Thanks for your help, and I am sorry if the answer is obvious...

0 Answers0