数据结构想实现链表用遍历的方式输出(那个写的pr函数),目前报错
No matching member function for call to 'traverse'
如何改正,先谢谢了
#include <iostream>
using namespace std;
template<typename T>struct PR{
void operator()(T &e) { cout << e << " "; }
};
template <typename T> struct ListNode {
int data;
ListNode<T> *pred;
ListNode<T> *succ;
ListNode() {}
ListNode(int e, ListNode<T> *p = NULL, ListNode<T> *s = NULL)
: data(e), pred(p), succ(s) {}
};
template <typename T> class List {
private:
int _size;
ListNode<T> *header;
ListNode<T> *trailer;
public:
void init();
int getsize() { return _size; }
List() { init(); }
template <typename VST> void traverse ( VST& );
ListNode<T> *insertb(ListNode<T> *p, T const &e) {
ListNode<T> *x = new ListNode<T>(e, p->pred, p);
x->pred->succ = x;
x->succ->pred = x;
return x;
}
ListNode<int> *first() { return header->succ; }
};
template <typename T> void List<T>::init() {
header = new ListNode<T>;
trailer = new ListNode<T>;
header->pred = NULL;
header->succ = trailer;
trailer->pred = header;
trailer->succ = NULL;
_size = 0;
}
template <typename T> template<typename VST>
void List<T>::traverse(VST &visit) {
for (ListNode<T> *p = header->succ; p != trailer; p = p->succ) {
visit(p->data);
}
}
template <typename T>void pr(List<T> &l){
l.traverse(PR<T>());
}
int main() {
List<int> list;
list.init();
ListNode<int> *f = list.first();
list.insertb(f, 3);
list.insertb(f, 34);
list.insertb(f, 13);
list.insertb(f, 39);
list.insertb(f, 23);
pr(list);
return 0;
}