【发布时间】:2021-03-12 17:13:51
【问题描述】:
我正在尝试制作一个链接列表。我有 3 个课程 - Employee、ListOfEmployee 和 NodeofEmployee。所有的函数都存储在一个头文件中。
员工类
class Employee {
friend class NodeofEmployee;
public:
Employee();
Employee(string n, double s);
private:
string name;
double salary = 0;
};
ListOfEmployee 类
class ListOfEmployee {
private:
NodeofEmployee* head; //getting point to NodeofInt class
public:
ListOfEmployee(); //constructor
~ListOfEmployee(); //destructor
void insertAtfront(string, double); //insert value to the back
void getSalary(string name); //prints of value
int deleteMostRecent(); //delete last value
void display();
friend ostream& operator <<(ostream& outputStream, const ListOfEmployee& e);
};
NodeofEmployee 类
class NodeofEmployee {
friend class ListOfEmployee;
public:
NodeofEmployee(int id, string name);
private:
Employee emp;
NodeofEmployee* next; //the address of the next value
};
我想重载operator<<,但它不让我打印出员工姓名和薪水:
ostream& operator <<(ostream& outputStream, const ListOfEmployee& e) {
NodeofEmployee *newpptr = e.head;
while (!e.head) {
outputStream << newpptr;
}
return outputStream;
}
【问题讨论】:
-
请出示minimal reproducible example,您在哪里声明了您的运营商?你看到了什么错误?
-
我认为你需要一个
ostream& operator <<(ostream& outputStream, const NodeofEmployee & e) {我也希望outputStream << e.head;e.head 是一个指针。 -
然后是
operator<<forEmployee -
outputStream << *e.head;可能会有所帮助,否则你只会得到指针的地址 -
ostream& operator <<(ostream& outputStream, const ListOfEmployee& e) {可能应该遍历整个列表。打印每个节点,而不仅仅是头节点。
标签: c++ linked-list operator-overloading