C++重载友元函数无法访问私有成员
c++ overloaded friend function can't access private members
当前正在进行一个项目,我的教授要求我们超载流式提取和输入运算符。我已经复制了他给我们开始实施的标题。这是我的标题学生。H:
// @file student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student {
/** add all the setter and getter methods **/
/**
* @param is the input stream
* @param course the student object reference
* @return the input stream
*/
friend istream &operator >> (istream &is, Student &student);
/**
* @param os the output stream
* @param course the student object reference
* @return the output stream
*/
friend ostream& Student::operator << (ostream& os, const Student& student);
public:
Student();
Student(string firstName, string lastName, int id, char grade);
void setFirstName(string firstName);
string getFirstName();
private:
string firstName;
string lastName;
int id;
char grade;
};
#endif /* STUDENT_H */
这是我正在使用the file student.cpp
的定义#include "student.h"
#include <iostream>
using namespace std;
istream &operator >> (istream &is, Student &student) {
is >> student.firstName;
}
clion一直告诉我,firstname是私人的,因此无法访问,我是否有明显缺少的东西?我已经检查并仔细检查了我的格式,并将其移动了很多,我遇到了很多麻烦。
是的,我已经看过一个类似的标题为问题,他在何处遇到了他所使用的名称空间,尝试过并看不到结果。任何帮助都非常感谢。
我无法解释错误消息,但是该功能必须返回一个值。
istream &operator >> (istream &is, Student &student) {
return (is >> student.firstName);
}
修复了该问题以及缺少的构造函数等,以及main()
,它应该可以很好地编译。
P.S。将课堂学生放在您的命名空间中,所有您的命名空间,从来没有在标题文件中写using namespace std;
。
第一个问题是全局范围中的标头文件中的 using namespace std;
。这可能会导致麻烦,所以不要做。
我个人不喜欢这种模式,即流操作员是班级的朋友。所以我这样做:
class Student {
public:
Student();
Student(std::string firstName, std::string lastName, int id, char grade);
void setFirstName(std::string firstName);
string getFirstName();
std::istream &scan(std::istream &input);
std::ostream &print(std::ostream &output) const;
private:
string firstName;
string lastName;
int id;
char grade;
};
std::istream &operator>>(std::istream &input, Student &student) {
return student.scan(input);
}
std::ostream &operator<<(std::ostream &output, const Student&student) {
return student.print(output);
}
对于编译器,没有区别。此模式还为其他解决方案打开了门,例如静态多态性,常规多态性(在这种情况下,您不必为每个类别定义流operator(。
相关文章:
- C++ 传递指向对象的指针的指针,无法访问成员
- 从不同C++类的成员变量访问成员函数
- 使用 getter 访问成员变量C++
- 如何使用包含内部类的类实例有效地从内部类访问成员?
- 通过多个类访问成员时出错
- 如何在使用对象指针时访问成员函数
- 为什么相应成员不能正确访问成员函数指针
- 从两个不同类继承的非虚拟基类的访问成员
- 如何使用 gcc 内联汇编器代码访问成员变量
- 如何使用常数向量访问成员函数
- 访问成员函数中参数的类成员
- C++嵌套结构初始化和访问成员
- 通过指向班级第一个成员的指针访问成员是不确定的行为吗?
- 如何使用C STD :: SET中的迭代器访问成员功能
- 访问成员C 时,结构的向量生成了范围的错误
- 带指针的班级访问成员
- 从聚合类访问成员变量
- 如何访问成员变量 sqlite 回调
- ctypes/C++segfault访问成员变量
- 通过父类型的指针或直接从派生类访问成员函数的差异