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;
}
pb.
  • 321
  • 3
  • 4
  • 21
  • try this: https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm –  May 18 '18 at 08:34
  • Are you sure `operator<<(std::ostream&, SingleLinkedList const&)' this is what you get? SingleLinkedList should be a template. – JiaHao Xu May 18 '18 at 09:38

2 Answers2

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;
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;
Garf365
  • 3,619
  • 5
  • 29
  • 41
  • 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? – pb. May 18 '18 at 09:34
  • 1
    It's typical, just see https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration or https://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration 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 – Garf365 May 18 '18 at 09:36