【发布时间】:2013-11-10 06:48:18
【问题描述】:
为什么explicit vector (size_type n) 表单在类外有效,但在类内无效?
这样编译:
#include <vector>
int main() {
std::vector<int> vec_(3); // set capacity to 3
return 0;
}
但不是这个:
#include <vector>
class C {
public:
std::vector<int> vec_(3); // set capacity to 3
};
int main() {
return 0;
}
g++ --std=c++0x -Wall -Wextra -g a.cpp
a.cpp:5:27: error: expected identifier before numeric constant
a.cpp:5:27: error: expected ‘,’ or ‘...’ before numeric constant
为什么? :(
【问题讨论】:
-
这不是您在类中初始化数据成员的方式。谁教你的?
-
因为你通过类构造函数调用成员构造函数。
标签: c++ class vector constructor