0

I'm attempting to make a matrix class, and so far I've made a member that can make a vector of vectors, but I'm not sure how to go about resizing the vectors if need be.

EX: resizeArray(3,3) should return a 3x3 vector.

Also, for some reason when I call my default member, matrix(), I get an error that says... "Request for member numrows in myMatrix, which is of type matrix[][]" I'm not entirely sure what that's asking for and couldn't find a suitable answer elsewhere.

Thanks in advance

    #include <iostream>
#include <vector>
using namespace std;

template <typename Object>
class matrix
{
    public:
    matrix( ) : array( 1 )
    {
            rows = 1;
            cols = 1;
        for( int i = 0; i < rows; i++ )
            array[ i ].resize( cols );
    }       

    matrix( int rows, int cols ) : array( rows )
    {
        for( int i = 0; i < rows; i++ )
            array[ i ].resize( cols );
    }

    const vector<Object> & operator[]( int row ) const
    { 
        return array[ row ]; 
    }

    vector<Object> & operator[]( int row )
    { 
        return array[ row ]; 
    }

    int numrows( ) const
    { 
        return array.size( ); 
    }

    int numcols( ) const
    { 
        return numrows( ) ? array[ 0 ].size( ) : 0; 
    }

    void resizeArray(int rows, int cols)
    {

         }

    private:
        vector< vector<Object> > array;
        int rows;
        int cols;
};


int main()
{
    matrix<int> myMatrix(3,2);
    //matrix<int> myMatrix1();
    cout << myMatrix.numrows();
    cout << "\n";
    cout << myMatrix.numcols();
    system("PAUSE");
    return 0;
}
jww
  • 97,681
  • 90
  • 411
  • 885
clf01
  • 21
  • 2
  • You can get an idea from the exmaple i added to http://stackoverflow.com/questions/17259877/1d-or-2d-array-whats-faster/17260533#17260533 . (Note that it is a 1d Matrix class, though.) – Pixelchemist Sep 19 '14 at 22:43
  • That's a great link, but ultimately my problem is I don't know how to pass my matrix to my function and then resize it. I know I should be able to do it similarly to how I created the object, but I can't come up with a viable solution. – clf01 Sep 19 '14 at 23:02

2 Answers2

0

You probably want to avoid a vector of vectors in a matrix class like that. It leads to non contiguous memory allocation, which slows down execution. The inner vectors add unnecessary overhead, too. You could consider using a single vector of size n*m where n and m are number of rows and columns respectively. Resizing is easier, too.

I'm using gcc 4.8.1 and your code works.

Adam Kosiorek
  • 1,438
  • 1
  • 13
  • 17
0
#include <iostream>
#include <vector>
using namespace std;

template <typename Object>
class matrix
{
public:
   matrix( )
       : array( 1 )
   {
       rows = 1;
       cols = 1;
       for( int i = 0; i < rows; i++ )
           array[ i ].resize( cols );
   }       

   matrix( int rows, int cols ) // you forgot to assign the members rows and cols
       : array( rows ), rows(rows), cols(cols)
   {
       for( int i = 0; i < rows; i++ )
           array[ i ].resize( cols );
   }

   const vector<Object> & operator[]( int row ) const
   { 
       return array[ row ]; 
   }

   vector<Object> & operator[]( int row )
   { 
       return array[ row ]; 
   }

   int numrows( ) const
   { 
       return array.size( ); 
   }

   int numcols( ) const
   { 
       return numrows( ) ? array[ 0 ].size( ) : 0; 
   }

   void resizeArray(int rows, int cols)
   {
       this->rows = rows;
       this->cols = cols;
       array.resize(rows);
       for(int i = 0; i < rows; i++)
           array[i].resize(cols);
   }

private:
    vector< vector<Object> > array;
    int rows;
    int cols;
};
programmerjake
  • 1,794
  • 11
  • 15