gpt4 book ai didi

来自 cppreference.com 的 C++ 结构初始化

转载 作者:太空宇宙 更新时间:2023-11-04 14:39:00 27 4
gpt4 key购买 nike

我试图编译下面的代码,但我总是得到提到的错误:

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

编译:

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

错误:

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

如何修复错误?

最佳答案

您链接的代码是有效的 C11,而不是 C++11。由于 C 和 C++ 是完全不同的语言,因此不会使用 C++ 编译器进行编译。

live example on wandbox.org

关于来自 cppreference.com 的 C++ 结构初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47755456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com