1

I am trying to overload output operator.

Program compiles, but instead of output (list of nodes) it prints some address.

Could you explain me where I made mistake?

I am sure that displayList function is correct. If you see also something incorrect, please get me know.

EDIT: if I dereference myList, I get error: undefined reference to `operator<<(std::ostream&, SingleLinkedList const&)' Something is wrong with const?

full, executable code - https://codepad.remoteinterview.io/DPAXORFXKM

output:

Constructor called...
Display function:
10 --> 20 --> 30 --> 
Display - overloaded operator:
0x9152a10

Destructor called...

template <typename T>
class SingleLinkedList{
protected:
    struct Node{
        T data;
        Node * next;
        Node() : next(nullptr) {}
        Node(const int num) : data(num), next(nullptr) {}
    };

private:
    Node * head;
    Node * tail;

public:
    SingleLinkedList();
    ~SingleLinkedList();
    void insert(T const& value);
    void displayList(std::ostream& stream = std::cout);
    friend std::ostream& operator<<(std::ostream& os, const SingleLinkedList& list);
};

template <typename T>
void SingleLinkedList<T>::displayList(std::ostream& stream){
    Node * temp = nullptr;
    temp = head;

    while(temp!=nullptr){
        stream << temp->data << " --> ";
        temp = temp->next;
    }
    stream << std::endl;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const SingleLinkedList<T>& list){
    list.displayList(os);
    return os;
}

int main(){

    SingleLinkedList<int> * myList = new SingleLinkedList<int>();

    myList->insert(10);
    myList->insert(20);
    myList->insert(30);
    std::cout << "Display function:" << std::endl;
    myList->displayList();

    std::cout << "Display - overloaded operator:" << std::endl;
    std::cout << myList << std::endl;

    delete myList;
    return 0;
}
2
  • try this: tutorialspoint.com/cplusplus/cpp_overloading.htm Commented May 18, 2018 at 8:34
  • Are you sure `operator<<(std::ostream&, SingleLinkedList const&)' this is what you get? SingleLinkedList should be a template. Commented May 18, 2018 at 9:38

2 Answers 2

4

You are handing myList to std::cout. This variable is a pointer to the list.

To print out the list itself you have to dereference the pointer:

std::cout << "Display - overloaded operator:" << std::endl;
std::cout << *myList << std::endl;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried it before, but I get error: "undefined reference to `operator<<"
3

Many errors:

first, remove friend std::ostream& operator<<(std::ostream& os, const SingleLinkedList& list);, it's not necessary

second, change displayList to be const (indeed, this function doesn't modify list and it doesn't have to - in a general way, always mark function which haven't to modify instance as const - const functions can be called for const instance, as you have inside your operator<<):

void displayList(std::ostream& stream = std::cout) const;

and

template <typename T>
void SingleLinkedList<T>::displayList(std::ostream& stream) const{

and finally, display list instead of pointer to list by dereferencing it :

std::cout << *myList << std::endl;

2 Comments

Thank you so much. I am a little bit confused. How can I use function with operator overloading without declaration? To be honest it is the first time when I see const on the end of line. I have always see it at the beginning next to type. Is it typical or it is something special? I would like to read about it, do you recommend any resource?
It's typical, just see stackoverflow.com/questions/751681/… or stackoverflow.com/questions/3141087/… It just informs compiler that this function doesn't change instance, and so it can be called when instance isn't mutable (to simplify, when instance is const). Also, it generates compilation error when you try to modify instance in a const function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.