【问题标题】:I'm trying to overload << operator and print out emp.name我正在尝试重载 << 运算符并打印出 emp.name
【发布时间】:2021-03-12 17:13:51
【问题描述】:

我正在尝试制作一个链接列表。我有 3 个课程 - EmployeeListOfEmployeeNodeofEmployee。所有的函数都存储在一个头文件中。

员工类

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&lt;&lt;,但它不让我打印出员工姓名和薪水:

ostream& operator <<(ostream& outputStream, const ListOfEmployee& e) {
    NodeofEmployee *newpptr = e.head;
    while (!e.head) {
        outputStream << newpptr;
    }
    
    return outputStream;
}

【问题讨论】:

  • 请出示minimal reproducible example,您在哪里声明了您的运营商?你看到了什么错误?
  • 我认为你需要一个ostream&amp; operator &lt;&lt;(ostream&amp; outputStream, const NodeofEmployee &amp; e) { 我也希望outputStream &lt;&lt; e.head; e.head 是一个指针。
  • 然后是operator&lt;&lt; for Employee
  • outputStream &lt;&lt; *e.head; 可能会有所帮助,否则你只会得到指针的地址
  • ostream&amp; operator &lt;&lt;(ostream&amp; outputStream, const ListOfEmployee&amp; e) { 可能应该遍历整个列表。打印每个节点,而不仅仅是头节点。

标签: c++ linked-list operator-overloading


【解决方案1】:

所以这里(至少)有两个错误

ostream& operator <<(ostream& outputStream, const ListOfEmployee& e) {
    NodeofEmployee *newpptr = e.head;
    while (!e.head) {
        outputStream << newpptr;
    }
    return outputStream; 
}

第一个错误是newpptr 不是Employee,而是NodeofEmployee 指针。要打印您需要的Employee

        outputStream << newpptr->emp;

第二个错误是,尽管您编写了一个 while 循环,但实际上并没有循环您的列表。 newpptr 在你的循环中永远不会改变,循环条件是错误的。我想你想要这样的东西

    NodeofEmployee *newpptr = e.head;
    while (newpptr) {
        outputStream << newpptr->emp;
        newpptr = newpptr->next;
    }

第三个明显的问题是您没有为Employee 定义operator&lt;&lt;。所以你需要添加这个

class Employee {
    ...
    friend ostream& operator <<(ostream& outputStream, const Employee& e);
};

您将在最后的operator&lt;&lt; 中打印姓名和薪水。

    ostream& operator <<(ostream& outputStream, const Employee& e)
    {
        outputStream << e.name << ' ' << e.salary << '\n';
        return outputStream;
    }

【讨论】:

  • 有什么方法可以打印出员工名单中的员工详细信息
  • 您为什么要这样做?姓名和薪水在 Employee 中是私有的,因此首先您必须编写公共 getter 方法来访问这些值。然后您必须修改 Listofemployee 中的代码才能使用这些方法。所以这是可能的,但我宁愿看到像我写的代码一样的东西。
  • 既然我提到了它,我看到 NodeofEmployee 中的 empnext 也是私有的。所以我写的代码也行不通。您需要考虑一下您希望在您的课程中访问(以及如何访问)哪些数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 2021-10-29
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多