我想知道是否有人能发现我的结构声明和使用有什么问题。目前我有一个结构并希望将 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
最佳答案
在大多数情况下,数组都会衰减为指向数组第一个元素的指针,就像 xcords
和 ycords
的情况一样。您不能像这样初始化结构。因此,您必须显式初始化成员:
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/