为什么x成员变量不明确?

问题描述 投票:1回答:1

有人可以向我解释以下编译器错误,该错误表明'x'是歧义的引用?

class A {
    int x; // private here
};

class B {
public:
int x; // public here
};

class C : public A,B {

};

int main() {

    C c;
    c.x = 5; // here is the error

    return 0;
}
c++ inheritance multiple-inheritance
1个回答
0
投票

您的C包含两个x变量。一个继承自每个父类。因此,要分配给A::x还是B::x都模棱两可。仅仅因为一个不可访问并不意味着另一个将被自动选择。编译器无法知道您打算尝试分配给专用A::x(这将是另一个错误)还是公用B::x

© www.soinside.com 2019 - 2024. All rights reserved.