By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Template class operator= error

P: n/a
Folks,
I have been trying for a week but I cannot debug the following error:

Error E2285 ex5.cpp 141: Could not find a match for
'matrix<complex<double>>::operator =(complex<double>)' in function
main()

Here is the relevant portion of my template class

//************************************************** *************************

#include <iostream>
#include <string>
#include <vector>

template <class Type>
class matrix
{
private:
std::vector<matrix<Type> > _data ;
int _nrows ;
int _ncols ;

public:
matrix<Type> (int rows=1, int cols=1) ;
matrix<Type> (const matrix<Type>& rhs) ;
~matrix<Type> () {};

matrix<Type>& operator=(const matrix<Type>& rhs) ;

matrix<Type>& lookup (int row, int col) ;
const matrix<Type>& lookup (int row, int col) const ;

matrix<Type>& operator() (int row, int col)
{
return lookup (row, col) ;
}
const matrix<Type>& operator() (int row, int col) const
{
return lookup (row, col) ;
}

int rows () const
{
return _nrows ;
}
int cols () const
{
return _ncols ;
}

} ; // end matrix
//************************************************** ***************************

// Matrix constructor implementation
template <class Type>
matrix<Type>::matrix (int rows, int cols)
: _data (rows * cols), _nrows (rows), _ncols (cols)
{ }

template <class Type>
matrix<Type>::matrix (const matrix<Type>& rhs)
: _data (rhs._data), _nrows(rhs._nrows), _ncols(rhs._ncols)
{ }

// Matrix operator= implementation
template <class Type>
matrix<Type>& matrix<Type>::operator= (const matrix<Type>& rhs)
{
if (this != &rhs)
{
_data = rhs. _data ;
_nrows = rhs. _nrows ;
_ncols = rhs. _ncols ;
}
return *this ;
}

template <class Type>
matrix<Type>& matrix<Type>::lookup (int row, int col)
{
int linear_index = row * _ncols + col ;
return _data [linear_index] ;
}

template <class Type>
const matrix<Type>& matrix<Type>::lookup (int row, int col) const
{
int linear_index = row * _ncols + col ;
return _data [linear_index] ;
}

//************************************************** **************************

And here is the calling code:

matrix<complex<double> > Y (2, 2) ;

Y (0, 0) = complex<double> (300.0, 1) ;

What am I doing wrong? Can anyone please help?

Thank you very much,

Zenon
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Zenon" <ze****@comcast.net> wrote...
Folks,
I have been trying for a week but I cannot debug the following error:

Error E2285 ex5.cpp 141: Could not find a match for
'matrix<complex<double>>::operator =(complex<double>)' in function
main()

Here is the relevant portion of my template class

//************************************************** ************************
*
#include <iostream>
#include <string>
#include <vector>

template <class Type>
class matrix
{
[...]
matrix<Type>& operator() (int row, int col)
Don't you mean to make it

Type& operator() (int row, int col)

???

[...]
What am I doing wrong? Can anyone please help?


HTH

Victor
Jul 22 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote:
Don't you mean to make it

Type& operator() (int row, int col)

HTH


I wasn't sure what HTH stood for, so I did a search ...

HTH - Hope This Helps
HTH - Hand To Hand combat
HTH - Hand To Heart (I'm being honest)
HTH - Hard to Handle
HTH - Harry the Horse

I assume you meant Harry the Horse, but I'm not completely certain ... ;-)

David F
Jul 22 '05 #3

P: n/a
Zenon wrote:
Folks,
I have been trying for a week but I cannot debug the following error:

Error E2285 ex5.cpp 141: Could not find a match for
'matrix<complex<double>>::operator =(complex<double>)' in function
main()
.... next time, please post code I can cut and paste (include a
compilable - and runnable if applicable - main !)

Here is the relevant portion of my template class

//************************************************** *************************

#include <iostream>
#include <string>
#include <vector>

template <class Type>
class matrix
{
private:
std::vector<matrix<Type> > _data ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^
"matrix<T>" is a vector of itself ?

I don't think even Hilbert thought of this ...

int _nrows ;
int _ncols ;

public:
matrix<Type> (int rows=1, int cols=1) ;
matrix<Type> (const matrix<Type>& rhs) ;
~matrix<Type> () {};

matrix<Type>& operator=(const matrix<Type>& rhs) ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^

Cool - you allow them to be assigned.


matrix<complex<double> > Y (2, 2) ;

Y (0, 0) = complex<double> (300.0, 1) ;
you only allow Y (0, 0) = matrix<complex<double> > and now you want to
assign a complex ?

What am I doing wrong? Can anyone please help?


You probably intended that the type of the matrix element to be the
template parameter (i.e. Type).

i.e.
std::vector< Type > _data ;
Type& operator=(const Type& rhs) ;

Type& operator() (int row, int col);

etc

Jul 22 '05 #4

P: n/a
Gianni Mariani wrote:
"matrix<T>" is a vector of itself ?

I don't think even Hilbert thought of this ...


Never understimate mathematicians... :)

--
/**
* Mattia Belletti - Undergraduate student @ cs.unibo.it
* ICQ: 33292311 - email: mb******@cs.unibo.it
* IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
* Linux registered user 299762 @ Linux registered machine 213003
*/

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.