运算符“ <

问题描述 投票:-1回答:2

我在为双链表创建“ <

这是我的头文件:


#ifndef SORTEDLIST_H
#define SORTEDLIST_H

#include <iostream>

class SortedList {

private:
    typedef struct node {
        int data;
        node* next;
        node* prev;
    }*nodePtr;

    int theSize;

    nodePtr head;
    nodePtr tail;

public:
    SortedList();
    //~SortedList();
    void insertItem(int inData);
    bool deleteItem(int delData);
    friend ostream& operator <<(ostream& ot, const SortedList& sL);
    int size() const;
    bool empty() const;



};



#endif

这里是我的构造函数:

SortedList::SortedList() {
    //Set pointers equal to NULL
    head = NULL;
    tail = NULL;
    theSize = 0;

    head = new node; //create new node of 3 parts: head, data and prev
    tail = new node; //create new node of 3 parts: head, data and prev
    head->next = tail; //next partition points to tail
    head->prev = NULL; //not necessary to put in?
    tail->prev = head; //prev partition points to head
    tail->next = NULL; //not necessary to put in?
    /*temp->next = tail; //access the node the temp pointer is pointing to, set the 'next' part equal to tail*/

}

这是我无法使用的ostream重载函数:

ostream& operator<<(ostream& ot, const SortedList& sL)
{
    sL.nodePtr temp;
    temp = sL.head->next;
    while (temp != sL.tail) {
        ot << temp->data << " ";
        temp = temp->next;
    }
    ot << "\n";
}

它总是告诉我sL.NodePtr,sL.head和sL.tail无法访问。我确实将其设置为朋友功能,所以我不确定为什么。

c++ operator-overloading doubly-linked-list ostream
2个回答
0
投票

您的运算符重载不会返回任何内容,因此它具有undefined behavior


0
投票

operator<<的实现存在几个问题:

© www.soinside.com 2019 - 2024. All rights reserved.