0

Assuming that there is a Class called Solution:

class Solution{
private:
    int COL;
    int ROW;

    vector<vector <int>> grid(ROW, vector<int>(COL));
public:
    void setData();
};

Then put the definition of function setData()

void Solution::setData(){
    for (int i = 0; i < ROW; i++){
        for (int j = 0; j < COL; j++){
            grid[i][j] = 1;
        }
    }
}
  • Firstly, in the declaration of vector grid, ROW and COL is unread;
  • Secondly, if I revise the declaration of grid as vector<vector<int>> grid(100, vector<int>(100))(namely, define dimension of vector clearly), it then lose the feature of dynamic
  • Last, if I revise the declaration of vector grid, the programme would be interrupted when running setData()

Sincerely thank you for any suggestions!

thanks for you guys, I defined the constructor function:

Solution(){
    ROW = 100;
    COL = 100;
}

however, COL and ROW is also unreadable in definition of grid(vector<vector>)

thank you!

Mr.parker
  • 25
  • 1
  • 7
  • 2
    This calls for a constructor. How are `ROW` and `COL` to be decided? – doctorlove Dec 13 '21 at 09:24
  • 4
    You declare `grid` as a *function*, not a variable. – Some programmer dude Dec 13 '21 at 09:27
  • What about making a vector of size `ROW*COL` and handling the right indexing conversion when you access data by index? – GPhilo Dec 13 '21 at 09:27
  • Where do you think that you declare the "vector as a private member"? I do not see it. – Yunnosch Dec 13 '21 at 09:28
  • 2
    Please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and read about constructor *initializer lists*. That allows you to *initialize* (as opposed to assign to) member variables. – Some programmer dude Dec 13 '21 at 09:33
  • @GPhilo, thanks a lot, I thought what you said would be very promising, but if we define a vector of size`ROW*COL`, is it still a dynamic container? Really appreciate for your answer, thank you again; – Mr.parker Dec 13 '21 at 09:34
  • As long as you're using a vector, yes (you can set ROW and COL at runtime). – GPhilo Dec 13 '21 at 09:36
  • @Some programmer dude, really appreciate for your answer sir, but I'm a little confused that wasn't the type of `grid` a kind of variable? I guess we define vector variable outside the class also like this, `vector> grid(100, vector(100));`; Thanks a lot. – Mr.parker Dec 13 '21 at 09:38
  • 2
    grid looks like a function, cos it has the form `return_type name(stuff)` - See https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse – doctorlove Dec 13 '21 at 09:41
  • 1
    It's not allowed to define and initialize member variables using parentheses. Parentheses are only used for *function* declarations. To initialize member variables inline you either need to use curly braces `{}` or "assignment" syntax with `=`. This is to avoid the ambiguity that exists elsewhere between function declaration and variable initialization. – Some programmer dude Dec 13 '21 at 09:41
  • thanks for everyone, I need some time to study your answers, really appreciated!!! – Mr.parker Dec 13 '21 at 09:55

3 Answers3

1

Currently your definition of grid

vector<vector <int>> grid(ROW, vector<int>(COL));

looks rather like a function. State it's type and name, and initialise elsewhere to avoid this:

class Solution {
private:
    const int COL;
    const int ROW;

    vector<vector <int>> grid;
public:
    void setData();

    Solution() :
        ROW{ 10 },
        COL {5 },
        grid(ROW, vector<int>(COL))
    {}
};

I made the sizes const, cos they are for the lifetime of your Solution, I presume.

You can change the constructor to take the values for the row and column and pass them in, rather than use the magic number I chose.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

In your code ROW and COL are undefined and you try to use them in your function setData().

Try using simple functions to add data

AddRow(vecotr<int> rowValue) {
     grid.push_back(rowValue);
}

If you want to use a Set function you have to check validity.

SetRow(uint pos, vecotr<int> rowValue){
     if (pos<=grid.size())
          return;
     ...
}
cic
  • 106
  • 4
0

As you can fill the vector at the running time so let's change the setData function like:

class Solution{
private:
    int COL;
    int ROW;
    vector<vector <int>> grid;

public:
    void setData();

    Solution(int c, int r){
        COL = c;
        ROW = r;
    }
};

void Solution::setData(){
    vector <int> row;
    row.reserve(COL);
    for (int j = 0; j < COL; j++){
        row.push_back(1);
    }
    for (int i = 0; i < ROW; i++){
        grid.push_back(x);
    }
}

int main()
{
    Solution x(5,10);
    x.setData();
    return 0;
}

I tested it and it worked properly. If you don't want to put 1 for all items in your grid, change the first for loop as you desired and make them two nested loops in your setData function.