我目前正在尝试重载 '<<' 运算符,但一直收到此错误消息:
在函数 '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: 注意:候选是:std::ostream& operator<<(std: :ostream&, const 链表&)
重载的运算符函数在 Linkedlist 实现文件中被声明为友元成员函数,因为它将访问私有成员变量(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);
}
这是 Linkedlist 头文件中的函数原型:
friend std::ostream& operator <<(std::ostream& outs, const Linkedlist& source);
我已经爬网了,到目前为止还没有找到解决方案。任何建议都会很棒!