Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
Source code 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
//matrix.h #include <vector> template <class T> class CMatrix2D : public std::vector< std::vector<T> > { public: CMatrix2D(){} CMatrix2D(int rows, int cols) : std::vector< std::vector<T> >(){ resize(rows, cols); } CMatrix2D(int rows, int cols, T val) { resize(rows, cols, val); } void resize(int rows, int cols, T val = NULL); }; 
Source code 

1 2 3 4 5 6 7 8 9 10 11 12 13 
//matrix.cpp #include "matrix.h" template <typename T> void CMatrix2D<T>::resize(int rows, int cols, T val) { for(int j = 0; j < rows; j++) { std::vector<T> temp; temp.resize(cols, val); push_back(temp); } } 
Source code 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
//main.cpp #include <iostream> #include "matrix.h" using namespace std; int main (int argc, char * const argv[]) { CMatrix2D<double> m(3,3,4); for(int j = 0; j < m.size(); j++) { for(int i=0; i < m[j].size(); i++) cout<<m[j][i]<<"\t"; cout<<endl; } std::cout << "Hello, World!\n"; return 0; } 
This post has been edited 1 times, last edit by "wysota" (Jun 6th 2005, 3:56pm)
Source code 

1 2 3 4 5 6 7 
#include "matrix.h" template <typename T> void CMatrix2D<T>::resize(int rows, int cols, T val) { resize(rows, vector<T>(cols, val)); } 
This post has been edited 1 times, last edit by "Viro" (Jun 6th 2005, 5:31pm)
Source code 

1 2 3 4 5 6 7 8 9 
void CMatrix2D<T>::resize(int rows, int cols, T val) { for(int j = 0; j < rows; j++) { std::vector<T> temp; //temp.resize(cols, val); push_back(temp); } } 
This post has been edited 1 times, last edit by "Jesse" (Jun 6th 2005, 6:53pm)
Quoted
Originally posted by Viro
Basically, what I've done is encapsulated a vector in a vector. Thus my 2D matrix is made up of a vector< vector <T> >. In my code, push_back is called on the outer vector, while resize is called on the inner vector, if that makes sense.
Here is how I could change it to totally use resize, but it still crashes.
Source code
1 2 3 4 5 6 7 #include "matrix.h" template <typename T> void CMatrix2D<T>::resize(int rows, int cols, T val) { resize(rows, vector<T>(cols, val)); }
Quoted
Originally posted by Viro
I'm trying to create a 2D Matrix by inheriting from std::vector.
...
Source code
1 2 3 4 5 6 7 8 9 //matrix.h #include <vector> template <class T> class CMatrix2D : public std::vector< std::vector<T> > { public: ... };
Source code 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
template <class T> class CMatrix2D { typedef std::vector<std::vector<T>> Matrix; public: ... T& operator( unsigned int r, unsigned int c ); const T& operator( unsigned int r, unsigned int c ) const; ... private: Matrix _matrix; }; 
Quoted
Originally posted by Viro
That means that all the operations that apply to a vector, should apply to a 2D matrix too. Hence, I'm implementing it via inheritance.
Quoted
Originally posted by Viro
It said it couldn't find the resize method, eventhough resize is part of std::vector.
Source code 

1 2 3 4 5 6 
template <typename T> void CMatrix2D<T>::resize(int rows, int cols, T val) { vector<T> v(cols,val); this>resize(rows, v); } 
Source code 

1 2 3 4 5 6 
template <typename T> void CMatrix2D<T>::resize(int rows, int cols, T val) { vector<T> v(cols,val); vector< vector<T> >::resize(rows, v); } 
Forum Software: Burning Board®, developed by WoltLab® GmbH