I am new to C++ and I write code to see how friend functions work. Here are two classes and I ask the user in friend function for parameters, which if they are equal with the values of the member variables, to be displayed. And I can't access one of the private members - the array with the country names. In the function int elcountry(element &e, supply s)
I am trying to display the number of elements which are of a certain type and from a particular country. The error is that member supply::country, (s.country[i])
in elCountry function is inaccessible. I don't know how to use getCountry()
function to access the private array.
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;
}
The signature of two functions don't match, they're irrelevant, it's not friend
function at all.
friend int elCountry(element &e, supply &s);
~ ~
int elcountry(element &e, supply s){
~
Note that the names don't match either.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.