简体   繁体   English

"初始化类的静态数据成员时出错"

[英]An error of initializing a static data member of class

class A{
    private:
        int x;
        static A a;
        static int  i;
    public:
        A(int y){
            std::cout<<y<<std::endl;
        }
};
int x = 10;
A a(x);
int A::i = x;

This is a peculiar language quirk - the scope resolution on the left, in int A::i<\/code> , affects the lookup scope on the right, so that actually refers to the x<\/code> member of A<\/code> .这是一个特殊的语言怪癖——左侧的范围解析,在int A::i<\/code>中,会影响右侧的查找范围,因此它实际上是指A<\/code>的x<\/code>成员。

Either rename one of the variables, or specify the scope of the desired x<\/code> explicitly:重命名变量之一,或明确指定所需x<\/code>的范围:

int A::i = ::x;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM