struct comp {
long a;
vector<int> b(9);
bool c;
};
Run Code Online (Sandbox Code Playgroud)
错误:
code.cpp:67:19: error: expected identifier before numeric constant
code.cpp:67:19: error: expected ‘,’ or ‘...’ before numeric constant
Run Code Online (Sandbox Code Playgroud)
这有什么问题?如果我说b有9个元素,为什么g ++不接受?
Ker*_* SB 10
因为C++不能那样工作.
初始化器位于构造函数的初始化列表中,例如
struct comp {
long a;
vector<int> b;
bool c;
comp() : b(9) { }
};
Run Code Online (Sandbox Code Playgroud)
(请注意,这样定义的类不再是聚合.)
注意:C++ 11添加了成员初始值设定项,但仅使用了复制初始化语法:
struct Foo {
int a = 5;
vector<char> b = vector<char>(8);
};
Run Code Online (Sandbox Code Playgroud)
编译器对此的支持仍然不完整.
| 归档时间: |
|
| 查看次数: |
2398 次 |
| 最近记录: |