【问题标题】:initializing of a struct of whose members are array of another struct [duplicate]初始化其成员是另一个结构的数组的结构[重复]
【发布时间】:2022-08-14 16:50:41
【问题描述】:

我有

#include <iostream>

typedef struct coordinate{
    double x;
    double y;
}point;

typedef struct sc_cell{ // single cell
    point sc[4];
}cell;

typedef struct sb_body { // for single body
    point sb[4];
}body;

using namespace std;

int main()
{
    body r_plate = {};
    
    r_plate.sb[0] = { 0,0 };
    r_plate.sb[1] = { 5,0 };
    r_plate.sb[2] = { 5,1 };
    r_plate.sb[3] = { 0,1 };


    return 0;
}

在此处主要的我已经初始化了r_plate4分,拿了4条线。有没有办法在一行中初始化它?

类似r_plate = { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } }(这显示错误太多初始值设定项)

    标签: c++ arrays struct initialization typedef


    【解决方案1】:

    结构body 是一个聚合,其中包含数据成员,而这些数据成员又是聚合。

    你需要写

    body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };
    

    那就是结构体包含一个数组,所以你必须写

    body r_plate = { { ... } };
    

    并且数组的每个元素都是结构类型的对象。所以你会有

    body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };
    

    以下初始化将不太可读但正确

    body r_plate = { { 0,0,5,0,5,1,0,1 } };
    

    body r_plate = { 0,0,5,0,5,1,0,1 };
    

    这是一个演示程序。

    #include <iostream>
    
    typedef struct coordinate{
        double x;
        double y;
    }point;
    
    typedef struct sc_cell{ // single cell
        point sc[4];
    }cell;
    
    typedef struct sb_body { // for single body
        point sb[4];
    }body;
    
    using namespace std;
    
    int main()
    {
        body r_plate = { 0,0,5,0,5,1,0,1 };
    
        for ( const auto &p : r_plate.sb )
        {
            std::cout << "( " << p.x << ", " << p.y << " ) ";
        }
        std::cout << '\n';
    
        r_plate = { { 0,0,5,0,5,1,0,1 } };
    
        for ( const auto &p : r_plate.sb )
        {
            std::cout << "( " << p.x << ", " << p.y << " ) ";
        }
        std::cout << '\n';
    
        r_plate = { { { 0,0 }, { 5,0 } , { 5,1 }, { 0,1 } } };
    
        for ( const auto &p : r_plate.sb )
        {
            std::cout << "( " << p.x << ", " << p.y << " ) ";
        }
        std::cout << '\n';
    
        return 0;
    }
    

    程序输出为

    ( 0, 0 ) ( 5, 0 ) ( 5, 1 ) ( 0, 1 ) 
    ( 0, 0 ) ( 5, 0 ) ( 5, 1 ) ( 0, 1 ) 
    ( 0, 0 ) ( 5, 0 ) ( 5, 1 ) ( 0, 1 ) 
    

    至于这个任务

    r_plate = { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } };
    

    那么第一个内括号被认为是数组列表初始化的起点。由于结构只有一个数据成员(数组),因此除第一个之外的所有其他列表初始化都没有结构的相应数据成员。所以编译器发出错误。

    【讨论】:

      【解决方案2】:

      再多一个{ }。一个用于body,一个用于数组,一个用于point 结构。

      body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };
      

      https://godbolt.org/z/WKz1zf6Kn

      您可以拆分它们:

      对于身体:

      body bd = {0,0};
      

      对于一组身体:

      body bd_arr[4] = { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } };
      

      然后对于包含主体数组的struct

      body r_plate = { { { 0,0 },{ 5,0 },{ 5,1 },{ 0,1 } } };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-27
        • 1970-01-01
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 2018-12-16
        相关资源
        最近更新 更多