【发布时间】:2021-02-19 03:54:44
【问题描述】:
我正在尝试使用 g++ 执行以下代码并收到不完整的类型错误
#include <stdio.h>
struct try_main{
union{
struct try_inner_one{
int fl;
float g;
}one;
struct try_inner_two{
char a;
}two;
}un;
int chk;
};
void func(struct try_inner_one o){
printf("%d\n",o.fl);
}
int main(){
struct try_main z = {{1,2},3};
func(z.un.one);
return 0;
}
错误:
union.c: In function ‘void func(try_inner_one)’:
union.c:15:6: error: ‘o’ has incomplete type
void func(struct try_inner_one o){
^
union.c:15:18: error: forward declaration of ‘struct try_inner_one’
void func(struct try_inner_one o){
^
union.c: In function ‘int main()’:
union.c:20:16: error: parameter 1 of ‘void func(try_inner_one)’ has incomplete type ‘try_inner_one’
func(z.un.one);
上面的代码已经用 gcc 成功编译了
这个错误的原因是什么以及如何解决这个问题
谢谢
【问题讨论】:
-
您在寻找正确的 C 和 C++ 吗?还是让它在 C++ 中工作就可以了?
-
我正在寻找在 C++ 中工作的正确方法以及为什么 C 中没有出现错误
-
此错误是因为您编写了一个 C 程序,但试图将其编译为 C++。 C 和 C++ 是两种完全不同的语言。
标签: c++ scope declaration unions qualified-name