Reputation: 21
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!
Upvotes: 0
Views: 215
Reputation: 42
A suggestion for future posts that you might make: If you post parts of your code in your question, please post all relevant bits. Right now, it is not clear what fetchData()
returns.
From the looks of it seems that fetchData()
returns something which does not have an overload for std::ostream::<<
.
When you output to a std::ostream
instance, all components must have defined behaviour with it. For instance, in the example below, for <<
to work with the instance of A
, the operator has to either use x
directly or B
needs to define some behaviour where it pushes some values known to the standard ostream
operator. Otherwise, you have to provide overloads for everything within the chain.
class B
{
friend std::ostream& operator<<(std::ostream& stream, const B& b);
int x = 10;
};
class A
{
private:
B b;
friend std::ostream& operator<<(std::ostream& stream, const A& a);
};
std::ostream& operator<<(std::ostream& stream, const B& b)
{
stream << b.x;
return stream;
}
std::ostream& operator<<(std::ostream& stream, const A& a)
{
stream << a.b;
return stream;
}
int main()
{
A a;
std::cout << a << "\n";
return 0;
}
My suspicion is that fetchData()
returns a type that does not have an overload for the <<
operator. This is why you see a failure.
Only 2 things that you can do
fetchData()
has an overload for <<
. C++ native and string types (in <string>
) already have this feature. If it is a custom type, then you have to write a overload for that type.fetchData
. That way you can utilise the std::string
overload for <<
in <string>
.As I said before, without knowing what fetchData()
returns, it is hard to say what is wrong here.
Upvotes: 0
Reputation: 1105
Is your cursor->fetchData() returning a std::string? If so, you must #include <string>
.
or, try
outs << cursor->fetchData().c_str() << " ";
Upvotes: 0