帮助声明 C++ 结构体,其中一个浮点数组作为其成员之一

发布于 2024-07-14 16:49:33 字数 817 浏览 6 评论 0原文

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

我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-07-21 16:49:33

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

Arrays decay to pointer-to-first-element of the array in most contexts as is the case with xcords and ycords. You cannot initialize the struct like this. So, you have to initialize the members explicitly:

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

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?

瀟灑尐姊 2024-07-21 16:49:33

尝试

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};

Try

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};
绝對不後悔。 2024-07-21 16:49:33

我认为您期望初始化将每个数组的元素复制到您的结构中。 尝试单独初始化结构中的数组元素,例如使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文