4 Replies - 1658 Views - Last Post: 08 July 2013 - 08:16 AM Rate Topic: -----

#1 sheilaEGR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-July 13

"error: no match for operator=" in C++ operator overload

Posted 07 July 2013 - 11:20 PM

Hi! I need your help to correct an error I don't understand. I'm writing a class for matrices handling. Basically this is my class code:

/////////////////////////////////////////////////
template <class T>
class matrix
{
public:
	matrix();
	matrix(int rows, int cols);
	~matrix();
	
	bool create(int rows, int cols);
	void destroy();
	
	bool set(int r, int c, T val);
	T get(int r, int c);
	
	T& operator()(int r, int c);
	void operator=(matrix &X);
	matrix operator+(matrix &X);
	
	void print();

	T *data;
	int rows,cols,tam;
};

template <class T> T& matrix<T>::operator()(int r, int c)
{
	return data[r*cols + c];
}

template <class T> void matrix<T>::operator=(matrix &X)
{
	T *ptr1,*ptr2;
	int i;
	create(X.rows,X.cols);
	ptr1 = data;
	ptr2 = X.data;
	for(i=0; i<tam; i++)
	{
		*ptr1 = *ptr2;
		ptr1++;
		ptr2++;
	}
	
}

template <class T> matrix<T> matrix<T>::operator+(matrix &X)
{
	int i;
	T *ptr1,*ptr2,*ptr3;
	matrix Y;
	
	if(X.rows != this->rows || X.cols != this->cols)
		return Y;
	
	Y.create(rows,cols);
	ptr1 = data;
	ptr2 = X.data;
	ptr3 = Y.data;
	for(i=0; i<tam; i++)
	{
		*ptr3 = *ptr1 + *ptr2;
		ptr1++;
		ptr2++;
		ptr3++;
	}
	
	return Y;
}
//////////////////////////////////////////////////////////


And the main code is:

#include <iostream>

#include "matrix.h"

int main()
{
	matrix<double> A(2,3);
	matrix<double> B(2,3);
	matrix<double> C;
	
	double x = 0.1, y = 0.9;
	for(int i=0; i<2; i++)
		for(int j=0; j<3; j++)
		{
			A(i,j) = x;
			B(i,j) = y;
			x += 0.2;
			y -= x;
		}
	C = A+B;        // ERROR ON THIS LINE!!
	
	printf("A:\n");		A.print();
	printf("\nB:\n");	B.print();
	printf("\nC:\n");	C.print();
	return 0;
}
//////////////////////////////////////////////////////////


The compilation error I get is:

//////////////////////////////////////////////////////////
tests/testMatrix.cpp: In function ‘int main()’:
tests/testMatrix.cpp:20: error: no match for ‘operator=’ in ‘C = matrix<T>::operator+(matrix<T>&) [with T = double](((matrix<double>&)(& B)/>/>/>/>))’
include/matrix.h:22: note: candidates are: void matrix<T>::operator=(matrix<T>&) [with T = double]
make: *** [tests/testMatrix.o] Error 1
//////////////////////////////////////////////////////////

Can you tell me what I'm doing wrong? If you need any extra information to solve it, please let me know.
I appreciate your help.


Is This A Good Question/Topic? 0
  • +

Replies To: "error: no match for operator=" in C++ operator overload

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: "error: no match for operator=" in C++ operator overload

Posted 08 July 2013 - 04:45 AM

I don't have a compiler available at the present time but I think the problem is in the following line snippet:
template <class T> matrix<T> matrix<T>::operator+(matrix &X)
{
	int i;
	T *ptr1,*ptr2,*ptr3;
	matrix Y;


matrix is a template class, you need the type everytime you try to use this class. Look at that last line, where is the type?


Jim
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: "error: no match for operator=" in C++ operator overload

Posted 08 July 2013 - 05:09 AM

There also seems to be a const problem. In the operator= overload the parameter should be const.

Also why are you using printf() in this C++ program instead of the C++ streams? And if you do insist in using this function you need to include the <cstdio> include file.

Jim

This post has been edited by jimblumberg: 08 July 2013 - 05:10 AM

Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: "error: no match for operator=" in C++ operator overload

Posted 08 July 2013 - 05:36 AM

View Postjimblumberg, on 08 July 2013 - 01:45 PM, said:

matrix is a template class, you need the type everytime you try to use this class.


You don't actually. Inside a member function of a template class Foo<T>, you can just write Foo to refer to Foo<T>. You'd only need to specify the template arguments if you wanted to use different ones.

Also if that were the error, the error message would look quite different.

View Postjimblumberg, on 08 July 2013 - 02:09 PM, said:

There also seems to be a const problem. In the operator= overload the parameter should be const.


To expand on that a bit: The expression B + C does not result in an l-value, meaning you can't assign to it. If a function or operator takes a non-const reference as its argument, that means that it may modify its argument and thus may only be called with l-values as its argument. So expressions like B + C can't be passed to a function or operator that takes a non-const reference, which is why operator=(Matrix<T>&) is not an applicable candidate for A = B+C, but operator=(const Matrix<T>&) would be.

PS: It is generally expected that operator= returns a reference to the assigned type, not void.
Was This Post Helpful? 1
  • +
  • -

#5 sheilaEGR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-July 13

Re: "error: no match for operator=" in C++ operator overload

Posted 08 July 2013 - 08:16 AM

Problem solved, thank you so much!! If someone else wants to know the solution, the new definition of my template is

template <class T>
class matrix
{
public:
	matrix();
	matrix(int rows, int cols);
	~matrix();
	
	bool create(int rows, int cols);
	void destroy();
	
	bool set(int r, int c, T val);
	T get(int r, int c);
	
	T& operator()(int r, int c);
	void operator=(const matrix &X);     // const parameter here
	matrix operator+(const matrix &X);   // and here
	
	void print();

	T *data;
	int rows,cols,tam;
};


The code is now perfectly working!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1