- 问题场景:
- 问题描述
- 代码:
- error:
- 原因分析:
- 解决方案:
在 cpp文件中, 使用 vector<结构体名称> xxx 来代替指针/数组 定义一串结构体 时。
问题描述 代码:#includeerror:#include using namespace std; struct node { int type; }; ... vector nodes[100]; ... int main() { ... nodes[0].type = 0; ... return 0; }
error: 'class std::vector
||=== Build: Debug in tmp (compiler: GNU GCC Compiler) ===| C:Usersmain.cpp||In function 'int main()':| C:Usersmain.cpp|28|error: 'class std::vector原因分析:' has no member named 'type'| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
我本来是想通过 vector 定义一个一位的 vector 数组的, 但由于之前都是用的数组,就习惯性的在变量名后填了[100], 所以会出错。
定义 vector nodes[100]; 那 nodes就相当于一个 二维数组了。
在使用 nodes[0] 时, nodes[0]是一个结构体数组, 不是一个结构体实例。 结构体数组没有 type 这个变量,所以 nodes[0].type = 0会报错。
使用一维结构体:
定义 vector
使用 不用修改。
使用二维结构体:
定义 vector
使用 需要修改将 nodes[index].type = 0 修改为 nodes[index_1][index_2].type = 0。



