0

I have this code:

in .h file:

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

in .cpp file

Vector2D Vector2D::operator*(float scalar, const Vector2D v2)
{
    return Vector2D(v2.m_x * scalar, v2.m_y * scalar);
}

when I try to compile it I get this error:

'Vector2D Vector2D::operator*(float, Vector2D)' must take either zero or one argument

what did I miss?!! thanks

Ok here is the full code: Vector2D.h

#ifndef VECTOR2D_H_INCLUDED
#define VECTOR2D_H_INCLUDED

#include <math.h>
#include <iostream>
class Vector2D
{
public:
    Vector2D(float x = 0, float y = 0): m_x(x), m_y(y) {}
    float getX()
    {
        return m_x;
    }
    float getY()
    {
        return m_y;
    }
    void setX(float x)
    {
        m_x = x;
    }
    void setY(float y)
    {
        m_y = y;
    }
    float length() { return sqrt(m_x * m_x + m_y * m_y); }

    Vector2D operator+(const Vector2D& v2) const;
    Vector2D& operator+=(Vector2D const& a);

    Vector2D operator*(float scalar);//float*Vector2D
    friend Vector2D operator*(float scalar, const Vector2D& v2);
    Vector2D& operator*=(float scalar);

    Vector2D operator-(const Vector2D& v2) const;
    Vector2D& operator-=(Vector2D const& a);

    Vector2D operator/(float scalar);
    Vector2D& operator/=(float scalar);

    void display()
    {
        std::cout << m_x << "," << m_y << std::endl;
    }
private:
    float m_x;
    float m_y;
};

and

Vector2D.cpp

#include "Vector2D.h"

//+ operator
Vector2D Vector2D::operator+(const Vector2D& v2) const
{
     return Vector2D(m_x + v2.m_x, m_y + v2.m_y);
}

Vector2D& Vector2D::operator+=(Vector2D const& a)
{
     m_x += a.m_x;
     m_y += a.m_y;
     return *this;
}

//- operator
Vector2D Vector2D::operator-(const Vector2D& v2) const
{
    return Vector2D(m_x - v2.m_x, m_y - v2.m_y);
}

Vector2D& Vector2D::operator-=(Vector2D const& a)
{
    m_x -= a.m_x;
    m_y -= a.m_y;
    return *this;
 }

 //* operator
 Vector2D Vector2D::operator*(float scalar)
 {
    return Vector2D(m_x * scalar, m_y * scalar);
 }

 Vector2D Vector2D::operator*(float scalar, const Vector2D& v2)
{
    return Vector2D(v2.m_x * scalar, v2.m_y * scalar);
}


 Vector2D& Vector2D::operator*=(float scalar)
{
     m_x *= scalar;
     m_y *= scalar;
     return *this;
}


 // / operator
 Vector2D Vector2D::operator/(float scalar)
{
    return Vector2D(m_x / scalar, m_y / scalar);
}
Vector2D& Vector2D::operator/=(float scalar)
{
   m_x /= scalar;
   m_y /= scalar;
   return *this;
}
2
  • Also, operator* needs to be a free function, not a member function like you're defining it. Jul 30, 2014 at 12:33
  • your implementation is not matching the declaration - you miss the &. can you post more code so we can help you?
    – NirMH
    Jul 30, 2014 at 12:33

1 Answer 1

6

In the .h file you are declaring the function as friend, however you define it as a member function in the .cpp. The .cpp declaration must look like

Vector2D operator*(float scalar, const Vector2D& v2) // you were also missing &

Also consider making operator* a template function (for type conversions),

template <typename T> 
friend Vector2D operator*(T scalar, const Vector2D& v2);

so you can silently accept other types of coefficients besides float (i.e. int or double etc)

2
  • There's no point making it a template; any numeric type can be implicitly converted to float. Jul 30, 2014 at 12:44
  • @MikeSeymour, was also thinking about using std::complex but otherwise yes you're right, I was overthinking it a bit :)
    – vsoftco
    Jul 30, 2014 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.