2012-08-26 66 views
0

我目前正试图超载“< <”运营商,但一直收到此错误信息:错误执行重载运算符时“<<” - C++

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&)

重载运算符函数声明为朋友在链表实现文件的成员函数,因为它会访问私有成员变量(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); 
} 

这是函数原型在链表头文件:

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

我已经抓取网络,目前还找不到解决方案。任何建议都会很棒!

回答

0

是你的cursor-> fetchData()返回一个std :: string吗?如果是这样,你必须#include <string>。 或试试

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