如何访问私有成员 - 具有友元函数的数组
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){
~
请注意,名称也不匹配。
相关文章:
- C++模板来检查友元函数的存在
- 如何使用单独文件中的派生类访问友元函数对象
- 模板化的类和友元函数
- 友元函数无法访问私有数据成员 (c++)
- 继承和友元函数,从基类访问受保护的成员
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- 在模板类之外定义友元函数的正确方法是什么?
- 2个模板化类的非模板友元函数未定义引用错误
- C++ 友元函数和私有构造函数
- 使第二个类的构造函数成为第一个类中的友元函数
- 未定义的类模板不会实例化以检查友元函数
- C++类中的友元函数有问题?
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 模板类中的模板友元函数
- C++ 17 个友元函数声明和内联命名空间
- 友元函数需要一个帮助程序函数
- 在类内定义的友元函数与类外定义的友元函数的查找规则之间的差异
- 运算符重载在 C++ 中使用友元函数
- 类C++友元函数无法访问封装的类
- 函数指针作为友元函数