2013-06-18 215 views
4
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,我們可以在課堂的任何地方爲朋友建模。爲什麼這個朋友不能訪問私有變量?

+0

你正在得到什麼確切的錯誤? – ereOn

回答

2

下面是一些代碼,工程:

#include <iostream> 
class Student 
{ 
public: 
Student(int test) : key(705) 
{ 
    if(test == key) 
    { 
     std::cout << "A student is being verified with a correct key: "<< test << std::endl; 
     allow=1; 
    } 
    else 
    { 
     std::cout << "Wrong key" ; 
    } 
} 

friend void printResult(Student* student); 

private: 
    const int key; 
    int allow; 
}; 

void printResult(Student* student) 
{ 
    if(student->allow==1) 
    { 
     std::cout<< " Maths: 75 \n Science:80 \n English: 75" << std::endl; 
    } 
} 

int main(int argc, char* argv[]) 
{ 
    int testkey; 
    std::cout << "Enter key for Bob: "; 
    std::cin >> testkey; 

    Student bob(testkey); 

    printResult(&bob); 
} 

我修改了它保留打印功能在全球空間(只基於它看起來像你想要的)。它需要一個Student *參數,因爲它被聲明爲朋友,它將看到「允許」變量。然而,這不是C++的一部分,你會想要濫用。小心你如何使用朋友。像這樣使用它在更大的代碼庫中是危險的。全球功能通常不是要走的路。將打印功能作爲學生課程中的公共成員功能可能是更好的做事方式。其他答案提供了顯示實現的代碼。我決定向你展示這個實現,因爲它似乎更接近你在你的問題中尋找的東西。

我也確保在引用cout和endl時使用'std ::'。這消除了'使用命名空間標準;'的需要在頂部。在更復雜的項目中,這只是良好的編程練習。

+1

感謝您的額外提示!我需要花些時間研究你所做的改變......非常有幫助的答案! – user2477112

+1

您可能要考慮的另一件事是以不同的方式進行輸入。一個裸cin >>可能會失敗,如果你給它意想不到的數據,扔掉你的程序的其餘部分。 –

5

這是因爲allow屬於類的一個實例,但沒有引用實例。

你或許應該讓printResult類的成員函數,而不是使之成爲一個外部函數使函數取到Student實例的引用,因此您可以通過instance.allow其中instance訪問allow成員的一個參數類型爲const Student&

5

printResult不是一個成員函數,所以你需要給它一個Student實例來執行。例如

void printResult(const Student& s) 
{ 
if(s.allow==1) 
{ 
    cout<< " Maths: 75 \n Science:80 \n English: 75" << endl; 
    } 
} 

然後

Student student(1); 
printResult(student); 
2

類朋友功能被允許訪問一個類的實例的成員。 你應該實例傳遞給函數,如:

void printResult(Student s) 
{ 
if(s.allow==1) 
{ 
    cout<< " Maths: 75 \n Science:80 \n English: 75" << endl; 
    } 
} 
+0

傳遞一個不需要的副本。使用'const Student'來代替函數。 (除了參數列表,你不需要改變任何東西) –

2
class Student{  
private: 
const int key; 
int allow;  
public: 
Student(int test) 
:key(705) 
{ 
    if(test == key) 
    {cout << "A student is being verified with a correct key: "<< test << endl; 
    int allow=1; 
    } 
    else 
    { 
     cout << "Wrong key" ; 
    } 
}  
friend void printResult(); 
void printResult() //<--- printResult() is a member function, so keep it inside the class 
{ 
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); 
bob.printResult(); // <--- you need to specify the owner of printResult() 
}