2

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

我的代码:

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
4

3 回答 3

3

尝试

Player player = {{1,1,1,1,1,1,1,1,1,1,1,1 },
                 {1,1,1,1,1,1,1,1,1,1,1,1 },
                 1,1,1,
                 2,2,
                 {},
                 true,true};
于 2009-03-09T18:46:20.410 回答
3

在大多数情况下,数组会衰减为指向数组的第一个元素的指针,就像xcordsand一样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] 的初始化程序。输入适当的值。否则会有默认初始化——这就是你想要的吗?

于 2009-03-09T18:50:14.440 回答
0

我认为您期望初始化将每个数组的元素复制到您的结构中。尝试单独初始化结构中的数组元素,例如使用for循环。

没有用于复制另一个数组元素的浮点数组的“构造函数”。

于 2009-03-09T18:47:42.340 回答