-1

I'm having trouble creating the overload function of "<<" for my doubly linked list.

Here is my header file:


#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

Here is my constructor:

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*/

}

and here is the ostream overload function I can't get to work:

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";
}

It keeps telling me sL.NodePtr, sL.head, sL.tail are inaccessible. I do have it set as friend function so I am unsure why.

MWeber
  • 21
  • 2
  • You don't return anything. – Nathanael Demacon Oct 22 '19 at 20:11
  • 1
    that and `return ot;` – Jeffrey Oct 22 '19 at 20:14
  • 2
    mWebber in case you are coming in from Java or C#, C++ uses `::` not `.`, to resolve scopes, and scope cannot be resolved from a variable, you have to resolve it based on type. Most of the time you know the type and if you don't, [use `decltype`](https://en.cppreference.com/w/cpp/language/decltype). – user4581301 Oct 22 '19 at 20:23
  • I tried your code and got different errors than you (`'ostream' does not name a type`, and after that was fixed: `invalid use of 'SortedList::nodePtr'`). My conclusion is that you have not provided sufficient context; you did not provide a [mcve]. – JaMiT Oct 22 '19 at 23:50

2 Answers2

1

There are several problems with your implementation of operator<<:

  • sL.nodePtr needs to be SortedList::nodePtr.

  • the while loop is all wrong. It is not accounting for an empty list, and it ignores the tail node of a non-empty list. Oh wait, your list makes use of dummy nodes for its head and tail, which is completely unnecessary and just complicates the class's design. Get rid of the dummies altogether.

  • it doesn't return anything. It needs to return ot.

Try this instead:

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

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

Alternatively, you can use a for loop instead of a while loop:

ostream& operator<<(ostream& ot, const SortedList& sL)
{
    for(SortedList::nodePtr temp = sL.head; temp; temp = temp->next) {
        ot << temp->data << " ";
    }
    ot << "\n";
    return ot;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

Your operator overload doesn't return anything so it has undefined behavior.

Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 1
    Might want to come back and revisit this answer. While this is true, the asker didn't get far enough to encounter the logical errors in the code. They got stopped by a compiler error. – user4581301 Oct 22 '19 at 20:20
  • Thankyou. It turned out my header file and .cpp did not both contain "using namespace std" so the friend part wasn't working, as it saw operator<< and operator<< in the two files as different things. – MWeber Oct 22 '19 at 20:20
  • However, despite adding namespace in, sL.temp isn't working while sL.head are. sL is highlighted as "not a modifiable L value." – MWeber Oct 22 '19 at 20:21
  • 1
    @MWeber See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/). And there is no `temp` member in your `SortedList` class, so `sL.temp` doesn't exist. – Remy Lebeau Oct 22 '19 at 20:24
  • That and `using namespace std` can be particularly harmful when placed in a header. Other people using your header may have written their code believing the contents of the `std` namespace have been safely walled off inside the `std` namespace, but as soon as they include your header, their code becomes infested with mystery errors. Once they figure it out, they drop by your cubicle and the conversation tends to get unpleasant. – user4581301 Oct 22 '19 at 20:27