コンパイラ エラー C2248
更新 : 2007 年 11 月
エラー メッセージ
'メンバ' : 'access' メンバ (クラス 'クラス' で宣言されている) にアクセスできません
派生クラスのメンバは、基本クラスの private メンバにアクセスできません。クラスのインスタンスの private メンバまたは protected メンバにアクセスできません。
C2248 の詳細については、サポート技術情報の文書 (KB243351) を参照してください。
次の例では C2248 エラーが生成されます。
// C2248.cpp
#include <stdio.h>
class X {
public:
int m_pubMemb;
void setPrivMemb( int i ) {
m_privMemb = i;
printf_s("\n%d", m_privMemb);
}
protected:
int m_protMemb;
private:
int m_privMemb;
} x;
int main() {
x.m_pubMemb = 4;
printf_s("\n%d", x.m_pubMemb);
x.m_protMemb = 2; // C2248 m_protMemb is protected
x.m_privMemb = 3; // C2248 m_privMemb is private
x.setPrivMemb(0); // OK uses public access function
}
テンプレートのフレンドおよび特殊な形式を使用すると、準拠の問題により C2248 が発生します。詳細については、「リンカ ツール エラー LNK2019」を参照してください。
// C2248_b.cpp
template<class T>
void f(T t) {
t.i; // C2248
}
struct S {
private:
int i;
public:
S() {}
// Delete the following line to resolve.
friend void f(S); // refer to the non-template function void f(S)
// Uncomment the following line to resolve.
// friend void f<S>(S);
};
int main() {
S s;
f<S>(s);
}
クラスのフレンドを宣言しようとした場合に、そのクラスがクラスのスコープ内のフレンド宣言から参照できない場合も、準拠の問題により C2248 が発生します。この場合のエラーを解決するには、フレンド宣言の外側のクラスにフレンドシップを与えます。詳細については、「Visual C++ 2005 コンパイラの互換性に影響する変更点」を参照してください。
// C2248_c.cpp
// compile with: /c
class T {
class S {
class E {};
};
friend class S::E; // C2248
};
class A {
class S {
class E {};
friend class A; // grant friendship to enclosing class
};
friend class S::E; // OK
};