更新:2007 年 11 月
錯誤訊息
'member' : 無法存取類別 'class' 中宣告的 'access' 成員
衍生類別的成員不能存取基底類別的 private 成員。您無法存取類別執行個體的 private 或 protected 成員。
如需 C2248 的詳細資訊,請參閱知識庫 (Knowledge Base) 文件 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 出現的另一個一致性問題是:使用了樣板 friend 和特製化。如需詳細資訊,請參閱連結器工具錯誤 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 出現的另一個一致性問題是:當您嘗試宣告類別的 friend,而在類別範圍中 friend 宣告看不見該類別時。在這種情況下,請授與封入類別 (Enclosing Class) 夥伴關係,以解決這項錯誤。如需詳細資訊,請參閱 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
};