C++ 结构和数组概念 (-Wmissing-braces])

标签 c++ arrays structure

我正在尝试做这样的事情..我有 3 个头文件

1. coordinates.h

typedef struct {
  float x;
  float y;
  float z;
}coordinates3D;

2.plane.h

#include "coordinates.h"
typedef struct{
    coordinates3D plane;
} plane3D;

3.pointArray.h
#include "plane.h"

plane3D points[] ={
    {1.0f,3.74f,0.2354f},
    {6.823f,9.234f,1.097f},
};

和一个cpp文件

4. main.cpp
#include "pointArray.h"
int main(int argc, char **argv)
{
    std::cout<<points[1].plane.x;
    std::cout<<points[0].plane.y;
}

一切正常,但我收到一条警告消息

warning: missing braces around initializer for 'coordinates3D' [-Wmissing-braces]

我不确定如何解决这个警告......

最佳答案

这是因为嵌套结构。只需在值周围添加几个大括号:

plane3D points[] ={
    { { 1.0f,3.74f,0.2354f } },
    { { 6.823f,9.234f,1.097f } },
};

最外层是数组,下一对是 plane3D 结构,再下一个是 coordinates3D 结构。

关于C++ 结构和数组概念 (-Wmissing-braces]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19355019/

相关文章:

c++ - 如何在 C++ 中创建 "wrapper"?

c++ - 如何使用 C++ 11 创建计时器事件?

javascript - ParseInt 数组中的元素为 int

javascript - 使用 'this' 关键字时数组的长度不同

python - 在numpy中用零填充数组

c++ - Qt 文档中的类分组列表在哪里?

c++ - c++中八进制数的行为

c - 努力从 C 中的结构中打印值

持有指向另一个结构的指针的 C 结构如何访问该内部结构指针的成员

c++ - (C/C++) 结构包含包含结构的 union ...?