Template class operator= error

Discussion in 'C++' started by Zenon, Dec 2, 2003.

  1. Zenon

    Zenon Guest

    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
    Zenon, Dec 2, 2003
    #1
    1. Advertising

  2. "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>>::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
    Victor Bazarov, Dec 2, 2003
    #2
    1. Advertising

  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>>::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) ;


    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
    Gianni Mariani, Dec 2, 2003
    #3
  4. Zenon

    David Fisher Guest

    "Victor Bazarov" <> 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
    David Fisher, Dec 2, 2003
    #4
  5. 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:
    * IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
    * Linux registered user 299762 @ Linux registered machine 213003
    */
    Mattia Belletti, Dec 2, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Replies:
    16
    Views:
    730
    Pete Becker
    May 4, 2005
  2. Gerhard Pfeiffer
    Replies:
    3
    Views:
    412
    Victor Bazarov
    Sep 14, 2006
  3. Replies:
    1
    Views:
    2,082
    Gianni Mariani
    Jun 8, 2007
  4. nguillot
    Replies:
    5
    Views:
    514
  5. A L
    Replies:
    1
    Views:
    502
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page