C++ compile time error: expected identifier before numeric constant

阅读 117

2022-01-30

vector - C++ compile time error: expected identifier before numeric constant - Stack Overflow

expected identifier before numeric constant错误_DXT的博客-CSDN博客

You cannot do this:

vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);

in a class outside of a method.

You can initialize the data members at the point of declaration, but not with () brackets:

class Foo {
    vector<string> name = vector<string>(5);
    vector<int> val{vector<int>(5,0)};
};

Before C++11, you need to declare them first, then initialize them e.g in a contructor

class Foo {
    vector<string> name;
    vector<int> val;
 public:
  Foo() : name(5), val(5,0) {}
};

Share

Follow

精彩评论(0)

0 0 举报