这是我的代码:
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
所以,这是我的问题。我需要做什么才能完成这项工作?我试图以这个问题为例。