读书人

哪位高手帮小弟我解决一下这个编译异常

发布时间: 2012-02-15 12:09:44 作者: rapoo

谁帮我解决一下这个编译错误
代码功能模拟STL的list类,实现迭代器,调用algorithm的find
编译错误,谁帮我看看呢
#include <iostream>
#include <algorithm>
using namespace std;

template <typename type> class Node {
private:
type item;
public:
Node *next;
Node(){}
Node(type a, Node* b):item(a), next(b){}
type& get_value() { return item; }
//Node* get_next() const { return next; }
};

template <typename type> class List {
private:
size_t len;
Node <type> *head, *tail;
public:
List() { len = 0; head = tail = NULL; }
size_t size() { return len; }
void push_front(type&);
void push_back(type&);
void display();
Node <type> * front() { return head; }
};

template <class type>
void List <type> ::push_back(type& item) {
if ( tail == NULL ) {
tail = head = new Node <type> (item,0);
return;
}
tail-> next = new Node <type> (item,0);
tail = tail-> next;
}

template <class type>
void List <type> ::push_front(type& item) {
Node <type> *temp = new Node <type> (item,0);
temp-> next= head;
head = temp;
if ( tail == NULL ) tail = head;
}

template <class type>
void List <type> ::display() {
Node <type> *p = head;
while ( p != NULL ) {
cout < < p-> get_value() < < endl;
p = p-> next;
}
}

template <class type> class Iterator {
public:
type *ptr;
explicit Iterator(type* a = NULL):ptr(a){}

type& operator *() const { return ptr-> get_value(); }
type* operator -> () const { return ptr; }

type value() { return ptr-> get_value(); }
Iterator& operator ++() {
ptr = ptr-> next;
return ptr;
}
Iterator operator ++(int) {
Iterator temp = *this;
++(*this);
return temp;
}

bool operator ==(const Iterator <type> & com) {
return (ptr == com.ptr);
}
bool operator != (const Iterator <type> & com) {
return (ptr != com.ptr);
}
};
int main( void ) {
List <int> li;
for ( int i = 0; i < 5; ++i ) {
li.push_back(i);
li.push_front(i);
}
li.display();
Iterator <Node <int> > begin(li.front());
Iterator <Node <int> > end;


Iterator <Node <int> > it = find(begin,end,3);
if ( it == end ) cout < < "Not Found!!! " < < endl;
else cout < < it-> get_value() < < endl;
return 0;
}

[解决办法]
#include <iostream>
#include <algorithm>
using namespace std;

template <typename type> class Node {
private:
type item;
public:
Node *next;
Node(){}
Node(type a, Node* b):item(a), next(b){}
type& get_value() { return item; }
//++++++++Add by Avi_Shen
bool operator ==(int i)
{
if(get_value() == i)
return true;
return false;
}
//+++++++++Add by Avi_Shen
//Node* get_next() const { return next; }
};

template <typename type> class List {
private:
size_t len;
Node <type> *head, *tail;
public:
List() { len = 0; head = tail = NULL; }
size_t size() { return len; }
void push_front(type&);
void push_back(type&);
void display();
Node <type> * front() { return head; }
};

template <class type>
void List <type> ::push_back(type& item) {
if ( tail == NULL ) {
tail = head = new Node <type> (item,0);
return;
}
tail-> next = new Node <type> (item,0);
tail = tail-> next;
}

template <class type>
void List <type> ::push_front(type& item) {
Node <type> *temp = new Node <type> (item,0);
temp-> next= head;
head = temp;
if ( tail == NULL ) tail = head;
}

template <class type>
void List <type> ::display() {
Node <type> *p = head;
while ( p != NULL ) {
cout < < p-> get_value() < < endl;
p = p-> next;
}
}

template <class type> class Iterator {
public:
type *ptr;
explicit Iterator(type* a = NULL):ptr(a){}
//++++++++Add by Avi_Shen
type& operator *() const { return *ptr;}//ptr-> get_value(); }
//++++++++Add by Avi_Shen
type* operator -> () const { return ptr; }

type value() { return ptr-> get_value(); }
//++++++++Add by Avi_Shen
type& operator ++() {
ptr = ptr-> next;
return *ptr;
}
type operator ++(int) {
Iterator temp = *this;
++(*this);
return temp;
}
//++++++++Add by Avi_Shen
bool operator ==(const Iterator <type> & com) {
return (ptr == com.ptr);
}
bool operator != (const Iterator <type> & com) {
return (ptr != com.ptr);
}
};
int main( void ) {
List <int> li;
for ( int i = 0; i < 5; ++i ) {
li.push_back(i);
li.push_front(i);
}
li.display();
Iterator <Node <int> > begin(li.front());
Iterator <Node <int> > end;
Iterator <Node <int> > it = find(begin,end,3);
if ( it == end ) cout < < "Not Found!!! " < < endl;
else cout < < it-> get_value() < < endl;
return 0;
}


楼主上面的我改好了...被改过的地方我做了点标识...你看一下...我这里是能运行的...

读书人网 >C++

热点推荐