C++ Double subscript overloading: cannot convert from 'type' to 'type &' -


i'm trying make 2d matrix class vector of vectors , both classes templates. i've overloaded subscript operator in vector class. problem occurs when try overload operator[] in matrix class error message: error c2440: 'return' : cannot convert 'vector' 'vector &'. here's code classes:

template <typename t> class vector { private:     t           *mem;     int         vectsize;  public:     vector<t>   (int _vectsize = 0);     //some other methods here     t& operator[](int index)     {             return mem[index];     } }; 

and

template <typename h> class matrix { private:     int dim;     vector< vector<h> > *mat;  public:     matrix<h> (int _dim = 0);      matrix<h> (const matrix & _copy);      vector<h>& operator[](int index)     {         return mat[index];  //here's error     } }; 

i made googling , found same examples or overloading () instead of []. can't understand why compiler can't see returned value mat[index] reference (which believe must reference). when working single vector subscript operator works fine though. please, point me mistake(s). in advance!

added: using non-dynamic vector seems solve current problem, instead of types' mismatch i've got 2 linker errors (unresolved external symbol). commenting , decommenting code found problem occurs if line vector< vector<h> > mat; or extend function present (it's empty method class vector). guess there's vector constructor, not know wrong.

template <typename t> //vector constructor vector<t>::vector(int _vectsize)     {         vectsize = _vectsize;         mem = new t[vectsize];         (int i=0; i<vectsize; i++)             mem[i] = 0;     } 

in matrix.h (it's not in separate files yet):

matrix<h> (int _dim = 0) : mat(_dim)     {         dim = _dim;         (int i=0; i<dim; i++)             mat[i].extend(dim-i);     } 

i'd love hear suggestions, if possible.

mat pointer vector. there no reason allocate dynamically vector, use vector<vector<h>> mat;.

also, operator[] has commonly 2 overloads: 1 const read, , 1 non-const write:

//read overload const t& operator[](std::size_t index) const  {     return /* blah blah */ }  //write overload t& operator[](std::size_t index)  {     return /* blah blah */ } 

this allows read vector in const contexts.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

debian - 500 Error upon login into Plesk Admin - auth.php3? -