Over loading * operator - must take either zero or one arguments
Asked Answered
R

4

14

I'm new to overloading operators, I did some search and found this helpful article, I wrote my own code like the author did but I get vector vector::operator*(float, vector) must take either zero or one argument error.

Here is my code:

class vector
{
      public:
       float x;
       float y;

      vector(float, float);
      float operator$ (vector, vector);
      vector operator* (float, vector);
      vector operator* (vector, float);
};

vector::vector(float _x = 0, float _y = 0)
{
   x = _x;
   y = _y;     
}
const float vector::operator$ (const vector &v1, const vector &v2)
{
    return (v1.x * v2.x) + (v1.y * v2.y);
}

const vector vector::operator* (const float &m, const vector &v)
{
    vector ret_val = v;
    ret_val.x *= m;
    ret_val.y *= m;
    return ret_val;
}

const vector vector::operator* (const vector &v, const float &m)
{
      return m * vector;     
} 

My operating system is kubuntu 12.04 and my IDE is dev-C++ running on linux using wine windows program loader.

Reareace answered 24/11, 2012 at 18:44 Comment(2)
First time I've ever seen someone overload $.....Daily
i tried to find a good operator for representing dot product in vectors, nothing good came up in my mind; can we define a new operator like what i did?Reareace
A
22

Because you are defining operator*() as a member function, there is already one implicit parameter: the object for which the method is invoked! Therefore, member functions take one explicit parameter, not two.

Audiophile answered 24/11, 2012 at 18:47 Comment(4)
we can access the implicit parameter by this. ..., am i right?Reareace
@EBi That's correct in that you can, but you really don't need to. this->x is equivalent to just x inside of a member function. The compiler will convert x to this->x.Audiophile
What if I need to keep order as MyClass = OtherClass * MyClass?Genealogy
@IC_: Then you have to override as a global method, and not a member of MyClass.Eserine
O
13

Just declare your operator overload function outside the class. Also you are returning a const which might not be what you want.

class foo {
  //code
};

foo operator*(foo& lhs, bar& rhs) const;
foo operator*(bar& lhs, foo& rhs) const;
Owlet answered 23/3, 2013 at 18:11 Comment(3)
why declare the operator overload function outside of the class?Frightened
This links explains the reason. Hope it helps. #4653432Owlet
I encountered the same problem with my custom complex numbers class! I am forced to define operators like that of the OP (eg. to do complex result = float number + complex number) outside the class. Otherwise I am getting the same error by gcc. However I find this very inelegant, even just stylistically, because if an operator logically belongs to a class, having to declare and define it ouside its class of pertinence does not make much sense imho. Thanks however to the OP for this question which I gave a +1Fuegian
G
0

However, the class name "vector" clashes with the standard library class with the same name. It is recommended to use a different class name, such as "Vector2D", to avoid naming conflicts. Also, the use of the dollar sign ($) for the dot product operator is non-standard and may be confusing for other programmers. It is recommended to use the dot operator (.) instead. Here's the corrected code:

    #include <iostream>
#include <cstring>

class Vector2D {
public:
    float x;
    float y;

    Vector2D(float, float);
    float operator*(const Vector2D &) const;
    friend Vector2D operator*(const Vector2D &, float);
    friend Vector2D operator*(float, const Vector2D &);
};

Vector2D::Vector2D(float _x, float _y) {
    x = _x;
    y = _y;
}

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

Vector2D operator*(const Vector2D &v, float m) {
    Vector2D ret_val = v;
    ret_val.x *= m;
    ret_val.y *= m;
    return ret_val;
}

Vector2D operator*(float m, const Vector2D &v) {
    return v * m;
}

int main() {
    Vector2D v1(1, 2);
    Vector2D v2(3, 4);

    std::cout << "v1 dot v2 = " << v1 * v2 << std::endl;

    Vector2D v3 = 2 * v1;
    std::cout << "2 * v1 = (" << v3.x << ", " << v3.y << ")" << std::endl;

    Vector2D v4 = v2 * 1.5;
    std::cout << "v2 * 1.5 = (" << v4.x << ", " << v4.y << ")" << std::endl;

    return 0;
}
Gripe answered 28/3, 2023 at 22:52 Comment(0)
G
-1

the answer given by chrisaycock is accepted but this is wrong answer. The statement is not correct. The number of explicit parameters a member function takes depends on the number of arguments required by the function.

Answer should be: Since the left-hand operand of this operator* (const float &m, const vector &v) is a float and not a vector object, it cannot be a member function of the Vector class. Therefore, this function is implemented as a non-member function outside the Complex class/or say friend function. Same goes to the const vector vector::operator* (const vector &v, const float &m) with difference now float at right-hand. Kindly verify

Gripe answered 28/3, 2023 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.