473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Template class operator= error

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
4 2010
"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
21
by: Makhno | last post by:
Hello, Why does my cast from Vector<class Float> to Vector<float> not work? It won't compile, template<class Float> class Vector { public: Vector(Float x1,Float y1,Float...
4
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code,...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
5
by: Fei Liu | last post by:
Hi, I have a interesting problem here, class absOP{ template<class T> T operator(T val) { return val < 0 ? -val : val; } }; Now the problem is I can't seem to use this overloaded operator, ...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
2
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.