模板化链表的ToString?

时间:2012-11-20 01:02:43

标签: c++ linked-list stdout

这是我的代码:

template<typename T>
class list {                                               
    private:                                               
        node<T>* head;                                     
        node<T>* tail;                                     
        int len;                                           
    public:                                                
        list(){                                            
            this->len = 0;                                 
            this->head = this->tail = 0;                   
        }                                                  
        ~list(){                                           
            node<T>* n = this->head;                       
            if (!n) return;                                
            node<T>* t = NULL;                             
            while (n){                                     
                t = n->next;                               
                delete n;                                  
                n = t;                                     
            }                                              
        }                                                  

        /* other stuff */

        ostream& operator<<(ostream &o, const list<T>& l) {
            node<T>* t = l.head;                           
            while (t){                                     
                strm << *(t->value);                       
                if (!t->next) break;                       
                strm << ", ";                              
                t = t->next;                               
            }                                              
            return strm;                                   
        }                                                  
};             

我收到以下编译错误:

rm bin *.o -f
g++ -g -Wall main.cpp -o bin
main.cpp:110: error: 'std::ostream& list<T>::operator<<(std::ostream&, const list<T>&)' must take exactly one argumentmain.cpp: In function 'int main(int, char**)':
main.cpp:151: error: no match for 'operator<<' in 'std::cout << l'
/usr/include/c++/4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
... other errors
make: *** [main] Error 1

所以,这是我的问题。我需要做些什么来完成这项工作?我试图以this question为例。

3 个答案:

答案 0 :(得分:2)

您的operator<<被声明为成员函数。你需要让它成为一个自由函数,即在类外定义它:

template <class T>
class list {
    // ...
};

template <class T>
ostream& operator<<(ostream &o, const list<T>& l) 
{
    // ...
};

如果您需要让operator<<成为班上的朋友,那么请查看我对this question

的回答

此外,我注意到您使用的ostream没有std::,这意味着您正在使用using namespace std

如果你这样做,那么打电话给你的班级list是一个非常糟糕的主意,因为如果{std::listusing namespace std#include <list>会被{{1}}拉入范围1}}将来会随时添加到文件中。

答案 1 :(得分:1)

你可以试着让运营商&lt;&lt;作为朋友?

....;
friend ostream& operator<<(ostream &o, const list<T>& l) {
....;

这是因为如另一个问题所示,这个函数必须声明为自由函数,如果声明为list的成员,只有在列表对象本身实际调用它时才使用它。


修改

@ je4d很好的捕获。

查看你的代码看起来你不需要操作员成为朋友,因为我猜测你将拥有头部和尾部的访问器。在类之外声明和定义它作为模板化的自由函数会更容易。

答案 2 :(得分:0)

我最终使用了boost::lexical_cast<std::string>和建议的ostream& operator <<方法的组合。

相关问题