2012-08-26 157 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() << " ";