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 ?