C++ Structure Initialization from cppreference.com

问题内容:

I was trying to compile the below code but I always get mentioned error:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char* Name;
    char* Branch;
} st_employee;

st_employee details[3] =
{
    [0].Name = "XXX",
    [0].Branch = "YYY",
    [1].Name = "ZZZ",
    [1].Branch = "PPP",
    [2].Name = "III",
    [2].Branch = "LLLL"
};

int main()
{
    printf("Hello world!");
    return 0;
}

Compile:

c++ -x c -std=c11  -O2 -Wall -Wextra -pedantic -pthread -pedantic-errors test.cpp -lm  -latomic  -Wmissing-field-initializers

Error:

test.cpp:44:5: warning: missing initializer for field 'Branch' of 'st_employee' [-Wmissing-field-initializers]
     [1].Name = "Chennai",
     ^
test.cpp:22:11: note: 'Branch' declared here
     char* Branch;
           ^
test.cpp:45:5: warning: missing initializer for field 'Branch' of 'st_employee' [-Wmissing-field-initializers]
     [2].Name = "Chennai"
     ^
test.cpp:22:11: note: 'Branch' declared here
     char* Branch;
           ^
test.cpp:46:1: warning: missing initializer for field 'Branch' of 'st_employee' [-Wmissing-field-initializers]
 };
 ^
test.cpp:22:11: note: 'Branch' declared here
     char* Branch;
           ^

How can I fix the error?

问题评论:

1  
Can you share link to the source?
1  
I think R2RT meant the cppreference.com page you based this on.
3  
Using a C++ compiler with -std=c11 is all kinds of weird.
6  
That page is clearly about C, not C++
1  
@Bob__: Ok, I found it. Not easy to see in all that crazy syntax.

答案:

答案1:

The code you linked is valid C11, not C++11. Since C and C++ are completely different languages, it won’t compile using a C++ compiler.

live example on wandbox.org

答案评论:

    
The use of printf is a bit of a giveaway too.
    
What is interesting clang handles it, but gcc does not: godbolt.org/g/oDjchC
    
Is it not possible to initialize in C++ like as mentioned in question – [0].Name etc?
– Prakash
1 hour ago
    
@Prakash: simply put, no.

答案2:

In C++11, if the struct contained const char*‘s, you could initialise the array like this:

st_employee details[3] =
{
    {"XXX","YYY"},
    {"ZZZ","PPP"},
    {"III","LLLL"}
};

答案评论:

    
No, conversion from string literal to char * is forbidden in C++.
    
Is it not possible to initialize like as mentioned in question – [0].Name etc?
– Prakash
1 hour ago
    
Hmm, fair point @O’Neil – now to figure out why my compiler doesn’t (at least) warn on that even with all warnings enabled.

答案3:

In addition to other answers, in C++ you really should prefer std::string over raw character pointers / arrays and std::array container over raw arrays. Then simply use aggregate initialization to initialize your elements:

#include <iostream>
#include <string>
#include <array>

struct st_employee {
    std::string Name;
    std::string Branch;
};

int main() {
    std::array<st_employee, 3> details = { "XXX", "YYY", "ZZZ", "PPP", "III", "LLLL" };
    for (auto el : details) {
        std::cout << el.Name << ' ' << el.Branch << '\n';
    }
}

答案评论:

原文地址:

https://stackoverflow.com/questions/47755456/c-structure-initialization-from-cppreference-com

Tags:,

添加评论