2

I'm very new to C++ and below is my program. It defines a nested vector.

#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
    vector<vector<int>> grid(10, vector<int>(10, 10000));
    return 0;
}

When I define the same vector as a class member, it reports syntax error:

#include <vector>

using namespace std;

class Test {
public:
    vector<vector<int>> grid(10, vector<int>(10, 10000));
};

int main(int argc, char *argv[]) {
    vector<vector<int>> grid(10, vector<int>(10, 10000));
    return 0;
}

How can I define a class member of type vector<vector<int>> and initialize it with 10 vector<int>(10, 10000)? I tried to declare the variable and initialize it in the constructor, but the compiler reports C:\my_projects\cpp1\main.cpp(8): error C2064: term does not evaluate to a function taking 2 arguments.

#include <vector>

using namespace std;

class Test {
public:
    Test() {
        grid(10, vector<int>(10, 10000));
    }

public:
    vector<vector<int>> grid;
};

int main(int argc, char *argv[]) {
    vector<vector<int>> grid(10, vector<int>(10, 10000));
    return 0;
}
Just a learner
  • 26,690
  • 50
  • 155
  • 234
  • 1
    `I'm very new to C++` This is why you should be using a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is complex, trying to learn it from miscellaneous internet sources and intuition is not going to get you very far, very quickly. – john Jan 05 '21 at 09:58

2 Answers2

3

You can use braced initializer or equal-sign initializer for default member initializer (since C++11), which doesn't support parentheses initializer; as the error message said, it attempts to be interpreted as function.

E.g.

class Test {
public:
    vector<vector<int>> grid{10, vector<int>(10, 10000)};
};

or

class Test {
public:
    vector<vector<int>> grid = vector<vector<int>>(10, vector<int>(10, 10000));
};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Thanks for the quick response! I have one more question. Why `vector numbers{20, 99L};` means initialize a vector with two initial values, but `vector> grid{10, vector(10, 10000)};` means 10 `vector(10, 10000)`? – Just a learner Jan 05 '21 at 10:26
  • @Justalearner Because `std::vector` has a [constructor](https://en.cppreference.com/w/cpp/container/vector/vector) taking `std::initializer_list` (#10 in the linked page), which is preferred when initialized with braced-initializer. `{10, vector(10, 10000)}` can't construct a valid `std::initializer_list`, then `vector> grid{10, vector(10, 10000)};` will use the #3 constructor. – songyuanyao Jan 05 '21 at 10:31
  • Thanks again for the clarification! – Just a learner Jan 05 '21 at 10:32
2

Your constructor initializer is wrong. It should be (initializer(s) after ':'). Example (Live):

class Test {
public:
    Test() : grid(10, vector<int>(10, 10000))
    {
         // grid(10, vector<int>(10, 10000)); error: this is not initializer
         // grid = vector<vector<int>>(10, vector<int>(10, 10000)) - this is ok
    }

public:
    vector<vector<int>> grid;
};

and for in-class member initialization you can use curly braces '{' or '=' to avoid most vexing parse problem (Live):

class Test {
public:
    vector<vector<int>> grid{10, vector<int>(10, 10000)};
};
StPiere
  • 4,113
  • 15
  • 24