Hi Guys, I am trying to teach myself templates in C++.
My current assignment is to create a matrix class by using vector of vectors. I have got most of it done but my code is crashing when I try to overload the random access operator in my matrix class. Here are the relevant code snippets
// This is the array class and I think this works fine
template<class V, class I =int>
class Array{
private:
vector<V> myStore;
public:
Array();
Array(int size);
Array(int size, V value);
~Array();
int size();
const V& element(I index);
V& operator[] (I index);
};
// This is the function definition for the Array random access operator
template<class V, class I> V& Array<V,I>::operator [](I index){
return this->myStore[index];
}
// This is the matrix class
template<class V, class I = int>
class Matrix{
private:
vector<Array<V,I> >myStore;
public:
Matrix();
Matrix(int rows, int columns);
Matrix(int rows, int columns, V value);
~Matrix();
int rows();
int columns();
V& operator() (int row, int column);
};
// This is the function definition for the Matrix random access operator.
template<class V, class I> V& Matrix<V,I>::operator ()(int row, int column){
Array<V,I> myArray = this->myStore[row];
return myArray[column];
}
My code does not crash when I return by value from the above function. What am I doing wrong. Any help is much appreciated