Leandro Lima
Leandro Lima

Reputation: 1170

Getting no match for operator overloaded

I overloaded the *= operator with this member function:

template<class U>
Matriz<T> & operator*=(const Matriz<U> & valor);

And also I have a constructor to matriz like this:

Matriz(const std::vector<T> & vector);

Well, I would like to make something like this:

double vetor[3] = { 1, 2, 3 };
std::vector<double> vec(vetor, vetor + 3);
Matriz<double> A("src//imatriz.dat"); //Creates a matrix with parameters from a file.
A*=vec;

That is, I would like to multiply a matrix by a vector. The problem is that compiler returns that there is no match for the operator.

---EDIT2---

As suggested, I also tried this:

template<class U>
friend Matriz<T> & operator*=(Matriz<U> & lhs, const Matriz<U> & rhs)

but A*=vec still doesn't work.

Any idea? If you need more code there's no problem to put it here.

Upvotes: 2

Views: 577

Answers (3)

Koushik Shetty
Koushik Shetty

Reputation: 2176

you could maybe do this

template<typename T, typename conv =  std::vector<T> > //for the Matriz class

now operator is

Matriz<T>& operator *= (const conv& vec); 

also the constructor as below

Matriz(const conv& vec);

EDIT 2:
you could do this otherwise

for the constructor use this

template<typename TYPE2MUL>
Matriz(const TYPE2MUL& var)

after this you can use

A *= vec; 

because it will call op as so operator *=(A,Matriz<vector>(vec)); no need for EDIT 1 or prior to that.

Upvotes: 1

billz
billz

Reputation: 45410

To make below statement work:

A *= vec;

You need another operator*= for vector type:

template<class U>
Matriz<U> & operator*=(const std::vector<U> & valor);

constructor will operate on new constructed object but will not convert existing object, for example, below statement should work:

A*=std::vector<double>(vetor, vetor + 3);

see live sample

Upvotes: 2

mumu
mumu

Reputation: 307

If you don't want to define another operator '=', the following code is OK.

A *= Matriz<double>(vec).

Upvotes: 0

Related Questions