1

I create a 2D vector (0,0) and would like to resize it (n,m), however, my resize function must remain const

I have tried doing

    void resize(int row, int col) const
    {
        array.resize(row, vector<int>(col));
    }

but keep getting

passing ‘const std::vector<std::vector<int>, std::allocator<std::vector<int> > >’ as ‘this’ argument discards qualifiers

How can I do this?

Matrix.h

#pragma once

#include <vector>

using namespace std;

template <typename Object>
class matrix
{

public:
    matrix(int rows, int cols) : array{ rows } {
        for (auto& thisRow : array)
            thisRow.resize(cols);
    }
    matrix( initializer_list<vector<Object>> lst ) : array( lst.size( ) )
    {
        int i = 0;
        for( auto & v : lst )
            array[ i++ ] = std::move( v );
    }
    matrix( const vector<vector<Object>> & v ) : array{ v } {}
    matrix( vector<vector<Object>> && v ) : array{ std::move( v ) } {}
    matrix() {}

    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 resize(int row, int col) const
    {
        array.resize(row, vector<int>(col));
    }

private:
    vector<vector<Object>> array;
};

main.cpp

    matrix<int> mat = matrix<int>();
    cout << "Zero-parameter matrix (rows,cols) = (" << mat.numrows() << "," << mat.numcols() << ")" << endl;

    mat.resize(4, 3);
    cout << "Resized matrix to 4x3" << endl;
    cout << mat << endl;
    mat[2][1] = 12;
    cout << "Modified (2,1)" << endl;
    cout << mat << endl;
asus
  • 1,427
  • 3
  • 25
  • 59
  • 1
    "my resize function must remain const" why? Thats an rather odd requirement. It is possible to do it but surely will confuse users, because nodody will expect that something that is constant resizes – 463035818_is_not_an_ai May 05 '20 at 20:37
  • Using `vector>` is not the best idea – Slava May 05 '20 at 20:40
  • It makes no sense to have a const method that changes the object. Why do you think it must be const? Either you've got a crazy professor who's giving you nonsensical assignments, or you are mistaken, probably. – john May 05 '20 at 20:44
  • I am "asssuming" I cannot change the function signature but I am trying to clarify that. I am just as confused but I agree that keeping it `const` is incorrect – asus May 05 '20 at 20:49
  • [Here is a really neat way to make a matrix](https://stackoverflow.com/a/2076668/4581301). Because all of the data is packed into one `vector` it is often much faster than the `vector` of `vector`s approach. You can't use `[]`, but that makes it harder for people to do dumb stuff like `mymatrix[i].clear();` – user4581301 May 05 '20 at 20:56

1 Answers1

3

When you put const on the end of the function there, you're saying the implicit this is const. That is, you're promising not to modify the state of the object this function is being called on.

But isn't the whole point of calling resize() to modify the state of the object? If I were you, I'd take the const off of there.

In other words, you have two options: either keep your promise to not change the state of the object (perhaps by returning a resized copy?), or lose the const.

scohe001
  • 15,110
  • 2
  • 31
  • 51