为什么我不能在类中初始化非const static成员或static数组?
class A
{
static const int a = 3;
static int b = 3;
static const int c[2] = { 1, 2 };
static int d[2] = { 1, 2 };
};
int main()
{
A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器发出以下错误:
g++ main.cpp
main.cpp:4:17: error: ISO C++ forbids in-class initialization of non-const static member ‘b’
main.cpp:5:26: error: a brace-enclosed initializer is not allowed here before ‘{’ token
main.cpp:5:33: error: invalid in-class initialization of static data …Run Code Online (Sandbox Code Playgroud) 我在结构中使用struct初始值,如下例所示.我无法理解的是为什么编译器不能char[] = { .. }像在变量声明中那样设置struct成员的大小?Clang ++给出以下错误:
错误:无法从类内初始化程序中推导出数组绑定
码:
struct A
{
char s[] = { 'F', 'O', 'O', ... };
};
Run Code Online (Sandbox Code Playgroud) 有谁知道这个错误是什么意思以及为什么在我尝试在结构中定义数组时会发生这种错误?
struct test{
int idk[] = { 1,2,3 };
};
Run Code Online (Sandbox Code Playgroud)
为什么数组 idk 是不完整类型之类的?
提前致谢。
附言。我需要这个,所以我可以从测试结构访问这些数组。