class Student{
public:
Student(int test)
:key(705)
{
if(test == key)
{cout << "A student is being verified with a correct key: "<< test << endl;
allow=1;
}
else
{
cout << "Wrong key" ;
}
}
friend void printResult();
private:
const int key;
int allow;
};
void printResult()
{
if(allow==1)
{
cout<< " Maths: 75 \n Science:80 \n English: 75" << endl;
}
}
int main()
{
int testkey;
cout << "Enter key for Bob: ";
cin >> testkey;
Student bob(testkey);
printResult();
}
函數printResult似乎無法從Student類訪問變量allow,它是private的。我是否在錯誤的地方對printResult進行了原型設計,或者語法錯誤? AFAIK,我們可以在課堂的任何地方爲朋友建模。爲什麼這個朋友不能訪問私有變量?
你正在得到什麼確切的錯誤? – ereOn