相关疑难解决方法(0)

为什么我不能在类中初始化非const静态成员或静态数组?

为什么我不能在类中初始化非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)

c++ static const

93
推荐指数
3
解决办法
9万
查看次数

标签 统计

c++ ×1

const ×1

static ×1