samedi 4 avril 2015

Strange behavior with std::vector

I am trying to implement a matrix template class using std::vector.


Code:



// matrix.h
template <class T>
class matrix
{
public:
~matrix(void);
matrix(int rows, int cols):_rows(rows),_cols(cols){ _size = _rows*_cols;init();} //

private:
matrix(void);
void init(); // sets up _matirx

//DATA
size_t _rows;
size_t _cols;
std::vector<T> _matrix;
}

// continued in matrix.tpp file
template <class T>
void matrix<T>::init(){
_matrix = std::vector<T>(_rows*_cols);
for(size_t i = 1; i <= _rows ;i++){
for(size_t j = 1 ; j <= _cols ; j++){
_matrix[(i - 1)*_rows + (j - 1 )] = 0 ;
}
}
}


template <class T>
matrix<T>::matrix(const matrix<T>& rhs)
{
_matrix = rhs._matrix;
_rows = rhs._rows;
_cols = rhs._cols;
}



//in Source.cpp

matrix<int> ABC = matrix<int>(4,2) ;
// Gives Debug Assertion Failed , subscript error in VS

matrix<int> ABC = matrix<int>(4000,4000) ;// Works , No Error

matrix<int> ABC = matrix<int>(2,4) ; // Works No Error


I know about using push_back , I will re implement the class using it , but I was wondering , why it works in the last two cases , and does not in the first case ? My hunch is that in the first case some elements are not being initialized. Is there a restriction in std::vector that say for index i , i+1 th element has to be initialized before the i+2 element is initialized ? Or is there something more subtle going on ?



  • Thank you


Aucun commentaire:

Enregistrer un commentaire