c++ - 帮助声明 C++ 结构,并将 float 组作为其成员之一

标签 c++ arrays structure declaration

我想知道是否有人能发现我的结构声明和使用有什么问题。目前我有一个结构并希望将 float 组存储为它的成员之一。

我的代码:

struct Player{
float x[12];
float y[12];
float red,green,blue;
float r_leg, l_leg;
int poly[3];
bool up,down;
};

然后我尝试填充结构:

float xcords[12] = {1,1,1,1,1,1,1,1,1,1,1,1 };
float ycords[12] = {1,1,1,1,1,1,1,1,1,1,1,1 };
Player player = {xcords,ycords,1,1,1,2,2,true,true};

错误:

1>.\template_with_console.cpp(35) : error C2440: 'initializing' : cannot convert from 'float [12]' to 'float'
1>        There is no context in which this conversion is possible
1>.\template_with_console.cpp(35) : error C2440: 'initializing' : cannot convert from 'float [12]' to 'float'
1>        There is no context in which this conversion is possible

最佳答案

在大多数情况下,数组都会衰减为指向数组第一个元素的指针,就像 xcordsycords 的情况一样。您不能像这样初始化结构。因此,您必须显式初始化成员:

Player player = {
        {1,1,1,1,1,1,1,1,1,1,1,1 }, // xcords
        {1,1,1,1,1,1,1,1,1,1,1,1 }, // ycords
        1,1,1,                      // red, green, blue
        2,2,                        // right, left
        {0,1,2},                    // poly[3]   -- missing?          
        true,true};                 // up, down

如果我理解正确的话,您还缺少 poly[3] 的初始化程序。输入适当的值。否则将会有默认的初始化——这是你想要的吗?

关于c++ - 帮助声明 C++ 结构,并将 float 组作为其成员之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/627512/

相关文章:

c++ - Netbeans C++ MinGW 设置

java - 将数组添加到另一个更大尺寸的数组

c - 错误 : assigning to 'int **' from incompatible type 'int [][]

mysql - 在 MySQL 中保存用户首选项的最有效方法是什么?

c++ - 我的模板重载 << 运算符有什么问题?

c++ - 将数据拟合到 3 次多项式

c - 指向函数的指针如何在 C 中发挥作用?

arrays - 用许多数组构建结构

c++ - 如何在 DirectX 9 中使用 D3DXSprite 尽可能快地绘制 D3DXFont?

java - 如何在android中不循环获取数组项?