1

I have a 2D vector underneath my private section; I'm trying to use the fill2dVector() function and allow a user to input the number of rows and columns. I was thinking of doing it two ways:

I tried doing something like this:

private
    size_t numRows, numCols;
    vector<vector<int> > data(numRows,vector<int> (numCols,0));  

But, that gives me an error: numRows is not a type, and VS wants me to write a function definition for data even though there was an option to do this automatically it would lead to more errors.

The second thing I tried was to just have a vector there and just resize it to whatever the user chose as their rows and columns. When I tried doing data.resize(numRows,numCols);, I got the error: no instance of overloaded function "std::vector<_Ty, _Alloc>::resize [with _Ty=std::vector<int, std::allocator<int>>, _Alloc=std::allocator<std::vector<int, std::allocator<int>>>]" matches the argument list

#include <iostream>
#include <vector>

class Matrix
{
public:
    Matrix();

    void fill2dVector();

    void display2dVector() const;

private:
    size_t numRows, numCols;
    std::vector<std::vector<int> > data;
};

Matrix::Matrix()
{
}

void Matrix::display2dVector() const
{
}

void Matrix::fill2dVector()
{
    std::cout << "   How many rows? ";
    std::cin >> numRows;

    std::cout << "How many columns? ";
    std::cin >> numCols;
    std::cout << std::endl;

    std::cout << "*** numRows = " << numRows << ", " << "numCols = " << numCols << std::endl;
    data.resize(numRows,numCols);
    std::cout << "*** rowSize = " << data.size() << ", " << "colSize = " << data[0].size() << std::endl;
}

int main()
{
    std::cout << "Enter the size of the matrix:" << std::endl;

    Matrix matrix;
    matrix.fill2dVector();

}
S. Coughing
  • 176
  • 1
  • 11
  • 2d vectors don't work like that, in C++. You have to resize() the first dimension to the number of rows, then iterate over it and resize() each row, individually. Or, some shortcuts can be made using `clear()` and `insert()`. For more information, see your C++ book, which should have a detailed chapter on using and initializing vectors; stackoverflow.com is not really a replacement for a C++ book. – Sam Varshavchik Apr 04 '20 at 23:53
  • @SamVarshavchik will look into it; currently, i've been reading articles on the web – S. Coughing Apr 05 '20 at 00:32
  • 1
    No, don't read "articles on the web". Read [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. C++ is the most complicated general purpose programming language in use today. Anyone can put together a web site that says anything that the clown that owns that web site wants to write. But it takes effort and money to write and publish a book, and nobody will do it without verifying the quality of the written material. – Sam Varshavchik Apr 05 '20 at 00:36

1 Answers1

2

You could overwrite data in fill2dVector,

data = vector<vector<int>>(numRows, vector<int>(numCols, 0));  
cigien
  • 57,834
  • 11
  • 73
  • 112