0
votes

I'm currently trying to overload the '<<' operator but keep receiving this error message:

In function 'std::ostream& operator<<(std::ostream&, const :Linkedlist&)': Linkedlist.cpp:148:34: error: no match for 'operator<<' in 'std::operator<< [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator](((std::basic_ostream&)((std::ostream*)outs)), ((const std::basic_string&)((const std::basic_string*)(& Node::node::fetchData()())))) << " "' Linkedlist.cpp:142:15: note: candidate is: std::ostream& operator<<(std::ostream&, const Linkedlist&)

The overloaded operator function is declared as a friend member function in the Linkedlist implementation file because it will access a private member variable (head_ptr):

std::ostream& operator <<(std::ostream& outs, const Linkedlist& source)
{
    node* cursor;
    for(cursor = source.get_head(); cursor != NULL; cursor = cursor->fetchLink())
    {
        outs << cursor->fetchData() << " ";
    }

    return(outs);
}

This is the function prototype in the Linkedlist header file:

friend std::ostream& operator <<(std::ostream& outs, const Linkedlist& source);

I've crawled the web and haven't been able to find a solution so far. Any advice would be great!

1

1 Answers

0
votes

Is your cursor->fetchData() returning a std::string? If so, you must #include <string>. or, try

outs << cursor->fetchData().c_str() << " ";