Template class operator= error

Z

Zenon

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>>::eek:perator =(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>::eek:perator= (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
 
V

Victor Bazarov

Zenon said:
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>>::eek:perator =(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
 
G

Gianni Mariani

Zenon said:
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>>::eek:perator =(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) ;


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
 
D

David Fisher

Victor Bazarov said:
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
 
M

Mattia Belletti

Gianni said:
"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: (e-mail address removed)
* IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
* Linux registered user 299762 @ Linux registered machine 213003
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top