non-trivial designated initializers not supported

前端 未结 6 1645
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 04:23

I have a structure as follows:

struct app_data
{
    int port;
    int ib_port;
    unsigned size;
    int tx_depth;
    int sockfd;
    char *servername;
           


        
6条回答
  •  盖世英雄少女心
    2020-12-24 05:19

    This does not work with g++. You are essentially using C constructs with C++. Couple of ways to get around it.

    1) Remove the "." and change "=" to ":" when initializing.

    #include 
    
    using namespace std;
    struct ib_connection
       {
        int x;
       };
    
       struct ibv_device
       {
       int y;
      };
    
    struct app_data
    {
        int port;
        int ib_port;
        unsigned size;
        int tx_depth;
        int sockfd;
        char *servername;
        struct ib_connection local_connection;
        struct ib_connection *remote_connection;
        struct ibv_device *ib_dev;
    
    };
    
    int main()
    {
    
        struct app_data data =
        {
            port : 18515,
            ib_port : 1,
            size : 65536,
            tx_depth : 100,
            sockfd : -1,
            servername : NULL,
    
            local_connection : {5},
            remote_connection : NULL,
            ib_dev : NULL
        };
    
       cout << "Hello World" << endl; 
    
       return 0;
    }
    

    2) Use g++ -X c. (Not recommended) or put this code in extern C [Disclaimer, I have not tested this]

提交回复
热议问题