[英]this declaration has no storage class or type specifier in union
I declared the static variable a in union and assigning it with 9 and I want to display 9 but it gives compiler error at "a = 9" this declaration has no storage class or type specified我在联合中声明了静态变量 a 并为其分配了 9 并且我想显示 9 但它在“a = 9”处给出编译器错误此声明没有指定存储类或类型
#include <iostream>
using namespace std;
union A{
static int a;
a = 9;
};
int main()
{
A a;
cout<<a.a;
return 0;
}
See this answer about the initial value ("default constructor") of a union
, and see this answer about static values in a union
, also, you can accomplish initializing the union like so:见这个答案有关的初始值(“默认构造函数”) union
,并看到这个答案是静态值的union
,也可以完成初始化工会就像这样:
#include <iostream>
using namespace std;
union A {
int a;
};
union B {
static int b;
};
int B::b;
int main() {
A a{9};
cout << a.a << endl;
B b;
cout << b.b << endl;
B::b = 18;
cout << b.b << endl;
return 0;
}
Output:输出:
$ ./union
9
0
18
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.