爲什麼explicit vector (size_type n)
表格在班級之外工作,但不在裏面? 這編譯:std :: vector ctor在類之外編譯,但不在裏面?
#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
爲什麼? :(
這不是你如何初始化類中的數據成員。誰教你的? – 0x499602D2
因爲您通過類構造函數調用成員構造函數。 – crashmstr