编译器错误 C2247

“identifier”不可访问,因为“类”使用“说明符”从“类”继承

identifier 是从使用专用或受保护访问声明的类继承的。

下面的示例生成 C2247:

// C2247.cpp
class A {
public:
   int i;
};
class B : private A {};    // B inherits a private A
class C : public B {} c;   // so even though C's B is public
int j = c.i;               // C2247, i not accessible

此错误还可能来自于为 Visual Studio .NET 2003 执行的编译器一致性工作:受保护成员的访问控制。 只能通过类 (B) 的成员函数访问受保护成员 (n),类 (B) 继承自 (n) 是其成员的类 (A)。

要使代码在 Visual Studio .NET 2003 和 Visual Studio .NET 版本的 Visual C++ 中有效,请将成员声明为该类型的友元。 还可以使用公共继承。

// C2247b.cpp
// compile with: /c
// C2247 expected
class A {
public:
   void f();
   int n;
};

class B: protected A {
   // Uncomment the following line to resolve.
   // friend void A::f();
};

void A::f() {
   B b;
   b.n;
}

C2247 还可能来自于为 Visual Studio .NET 2003 执行的编译器一致性工作:专用基类现在不可访问。 作为类型 (B) 的专用基类的类 (A) 不应供使用 B 作为基类的类型 (C) 访问。

要使代码在 Visual Studio .NET 2003 和 Visual Studio .NET 版本的 Visual C++ 中有效,请使用范围运算符。

// C2247c.cpp
// compile with: /c
struct A {};

struct B: private A {};

struct C : B {
   void f() {
      A *p1 = (A*) this;   // C2247
      // try the following line instead
      // ::A *p2 = (::A*) this;
   }
};