[英]How to access private member - array with friend function
我是C ++的新手,我編寫代碼來查看好友函數的工作方式。 這是兩個類,我向好友函數中的用戶詢問要顯示的參數(如果它們與成員變量的值相等)。 而且我無法訪問其中一個私有成員-具有國家/地區名稱的數組。 在函數int elcountry(element &e, supply s)
我試圖顯示來自特定國家的某種類型的元素的數量。 錯誤是無法訪問elCountry函數中的成員supply::country, (s.country[i])
。 我不知道如何使用getCountry()
函數訪問私有數組。
class element {
friend class supply;
private:
string name;
double value;
int serial;
public:
element();
int elCountry(element &e, supply &s);
double* nominals(element &e, supply &s);
string getName() {
return name;
}
int getSerial() {
return serial;
}
};
class supply {
private:
int serial;
string country[5];
int n;
public:
supply();
string* getCountry() {
string country = new string[5];
return country;
}
friend int elCountry(element &e, supply &s);
friend double* nominals(element &e, supply &s);
};
int elcountry(element &e, supply s){
string names, Country;
int n;
cout << "enter country = "; cin >> Country;
cout << "enter name of the element = "; cin >> names;
cout << "enter number of elements = "; cin >> n;
for (int i = 0; i < 5; i++) {
if (Country == s.country[i] && names == e.getName() && n == e.getSerial()) {
cout << "the country is " << count << endl;
cout << "the name is" << names << endl;
cout << "the number is " << n << endl;
}
}
return n;
}
兩個函數的簽名不匹配,它們不相關,根本不是friend
函數。
friend int elCountry(element &e, supply &s);
~ ~
int elcountry(element &e, supply s){
~
請注意,名稱也不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.