帮助声明 C++ 结构体,其中一个浮点数组作为其成员之一
我想知道是否有人能发现我的结构声明和使用有什么问题。 目前我有一个结构并希望将浮点数组存储为它的成员之一。
我的代码:
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
I was wondering if anyone could spot what is wrong with my structure declaration and use. At the moment I have a structure and wish to store float array as one of it's members.
My code:
struct Player{
float x[12];
float y[12];
float red,green,blue;
float r_leg, l_leg;
int poly[3];
bool up,down;
};
I then tried filling the struct:
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};
Error:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在大多数情况下,数组会衰减为指向数组第一个元素的指针,就像 xcords 和 ycords 的情况一样。 您不能像这样初始化结构。 因此,您必须显式初始化成员:
如果我理解正确的话,您还缺少 poly[3] 的初始化程序。 输入适当的值。 否则将会有默认的初始化——这是你想要的吗?
Arrays decay to pointer-to-first-element of the array in most contexts as is the case with
xcords
andycords
. You cannot initialize the struct like this. So, you have to initialize the members explicitly:You are also missing initializers for poly[3] if I understand correctly. Put in the appropriate values. Otherwise there will be default initialization -- is that what you want?
尝试
Try
我认为您期望初始化将每个数组的元素复制到您的结构中。 尝试单独初始化结构中的数组元素,例如使用
for
循环。浮点数组没有“构造函数”来复制另一个数组的元素。
I think you're expecting the initialization to copy the elements of each array into your structure. Try initializing the array elements in your structure individually, say with a
for
loop.There is no "constructor" for a float array that will copy the elements of another array.