ked*_*rps 0 c++ struct stl vector stdvector
我希望有一个包含向量的节点数据结构.我SIZE事先知道了向量的大小,因此我使用const说明符初始化变量.
vectorTest.cpp):#include <vector>
const int SIZE = 100;
struct node {
std::vector<int> myVec(SIZE); // ERROR: 'SIZE' is not a type
};
int main(int argc, char const *argv[]) {
std::vector<int> myVec(SIZE); // VALID
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g++ 5.4.0):g++ -std=c++1y vectorTest.cpp -o vectorTest
Run Code Online (Sandbox Code Playgroud)
在里面main(),一切都很好,我可以高兴地宣布:std::vector<int> A(SIZE);.但是,当我尝试在a中定义相同的内容时struct,我会收到错误消息'SIZE' is not a type.
我知道我可以这样做来int使用C风格的数组定义来声明s 的向量,
struct other_node {
int myVec[SIZE]; // VALID
};
Run Code Online (Sandbox Code Playgroud)
但我想知道为什么这是不可能的std::vector.
struct?这个错误是什么意思?
编译器期望行中的函数声明.因此,它期待一种类型而不是一种价值.
为什么我不能在一个内部声明预定义大小的向量
struct?
您可以使用:
// Legal but it initializes myVector with one element whose value is SIZE.
// std::vector<int> myVec{SIZE};
std::vector<int> myVec = std::vector<int>(SIZE);
Run Code Online (Sandbox Code Playgroud)