小编ked*_*rps的帖子

在结构中声明向量时,C++不是类型错误

我希望有一个包含向量的节点数据结构.我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)

COMPILE(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. …

c++ struct stl vector stdvector

0
推荐指数
1
解决办法
817
查看次数

标签 统计

c++ ×1

stdvector ×1

stl ×1

struct ×1

vector ×1