来自命名空间的Baseclass的虚拟继承,具有来自命名空间的私有成员

时间:2022-07-24 19:34:26

Why does the following code sample fail?

为什么以下代码示例失败?

namespace Base {
    class Base {
        protected:
            int x;
    };

    class BaseAlt {
    };
}

namespace Derived {
    class Derived : public virtual Base::Base {
        private:
            Base::BaseAlt baseAlt;
    };
}

int main() {
    return 0;
}

g++ compilation fails with the following error

g ++编译失败,出现以下错误

error: ‘BaseAlt’ in ‘class Base::Base’ does not name a type
             Base::BaseAlt baseAlt;

Why though?

1 个解决方案

#1


0  

The compiler is resolving to the parent class "Base::Base" which does not have an internal class named BaseAlt. The fully qualified name of the class BaseAlt is "::Base::BaseAlt." The additional "::" are required to differentiate between the namespace and the class.

编译器正在解析父类“Base :: Base”,它没有名为BaseAlt的内部类。 BaseAlt类的完全限定名称是“:: Base :: BaseAlt”。需要额外的“::”来区分命名空间和类。

#1


0  

The compiler is resolving to the parent class "Base::Base" which does not have an internal class named BaseAlt. The fully qualified name of the class BaseAlt is "::Base::BaseAlt." The additional "::" are required to differentiate between the namespace and the class.

编译器正在解析父类“Base :: Base”,它没有名为BaseAlt的内部类。 BaseAlt类的完全限定名称是“:: Base :: BaseAlt”。需要额外的“::”来区分命名空间和类。