-1

I'm trying to use an overloaded operator for a class with the following members and functions :

class MyVector {

  private:
    int           *data;
    unsigned int  vector_size;

  public:
    MyVector();
    MyVector(unsigned int n);
    MyVector(unsigned int size, int *in_array=NULL);
    MyVector(const MyVector& copy);
    ~MyVector();
    void print();

    // Assignment operator.
    MyVector& operator=(const MyVector& v);

    friend MyVector operator*(const int &lhs, MyVector &rhs);

    friend MyVector operator*(MyVector &rhs, int &lhs);

    MyVector operator*(MyVector &vector);
};

// Implementing operator overloading.
MyVector operator*(int &lhs, MyVector &rhs);
MyVector operator*(MyVector &lhs, int &rhs);

Inside one of my operator overloading functions, this is what I'm trying to do:

MyVector operator*(const int &lhs, MyVector &rhs) {

    unsigned int size = rhs.vector_size;

    // Create new vector.
    MyVector *v = new MyVector(static_cast<unsigned int>(size));

    for (unsigned int i = 0; i < rhs.vector_size; i++) {
        v -> data[i] = lhs * rhs.data[i];
    }

    return *v;
}

I'm getting the following error :

MyVector_fxns.cpp: In function 'MyVector operator*(const int&, MyVector&)':
MyVector_fxns.cpp:42:60: error: call of overloaded 'MyVector(unsigned int)' is ambiguous
  MyVector *v = new MyVector(static_cast<unsigned int>(size));

Before you mark this as a duplicate, I have checked this link and none of the solutions worked for me.

Out of the three possible constructors, only 1 should be called here. What is causing the error despite me passing the correct type in the constructor argument ?

Community
  • 1
  • 1
Objective-J
  • 319
  • 1
  • 2
  • 13
  • That code leaks memory. You allocated a new object, and you have no way of calling `delete` on the pointer value returned to you. Your `operator *` should be creating a `MyVector` object locally, and returning it by value. Get rid of the call to `new`, just `MyVector v(static_cast(size));...return v;` is all you need. – PaulMcKenzie May 06 '16 at 02:14

1 Answers1

3
   MyVector(unsigned int n);
   MyVector(unsigned int size, int *in_array=NULL);

This is ambiguous. Given the above constructors:

MyVector v(10);

This can use either one of these two constructors, with the second constructor's second parameter defaulted. That's what the compiler is complaining about. The compiler doesn't know which constructor to use. It can use either one. It can't decide on its own, or flip or coin, or something. When it comes to compiling C++ code, you can't just flip a coin, and pick a constructor.

The error message states literally this. It's a very clear compilation error:

call of overloaded 'MyVector(unsigned int)' is ambiguous

You have an overloaded constructor. And it's ambiguous.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • If there is only one argument why should it even bother with the second constructor ? I guess, removal of the default value of the `in_array` variable will fix it, but I just assumed it'll automatically consider the first constructor because there's only one argument. – Objective-J May 06 '16 at 01:44
  • 1
    @Objective-J Then why make the second argument optional in the second constructor, if it should use the first constructor when there's no second argument? – Barmar May 06 '16 at 01:45
  • Because the second constructor can also be called with one argument. If you declare a default parameter, and you don't provide it, you can still call the function, or the constructor, without it. That's how C++ works. If you want all constructor calls with one argument to call the first constructor, and all constructor calls with two arguments call the second one, then you must get rid of the "=null" default. – Sam Varshavchik May 06 '16 at 01:46
  • 1
    @Objective-J The error message serves an important purpose. If you meant to allow the second argument to default, then you made a mistake in defining a constructor with just one argument. It doesn't make sense to have both of these constructors, so it's not allowed. – Barmar May 06 '16 at 01:47