2011-06-23 57 views
-1

這是我做了很多更改後的代碼。我對函數(排序和查找)有問題,但我仍然無法理解如何解決它。排序和查找通用鏈接列表中的函數

using namespace std;  
using namespace ListExceptions; 

template <typename T> class Node; 
template <typename T> class Iterator; 

/*List class*/ 
template <typename T> 
class List{ 
private: 
Node<T> * first; 
Node<T> * last; 
int size; 
friend class Predicate; 
friend class Compare; 
public: 
typedef Iterator<T> iterator; 
List(){ 
    first = NULL; 
    last = NULL; 
    size = 0; 
} 
List(const List<T> & l); 
void pushBack(T element); 
void insert(const T& element, iterator i); 
iterator remove(iterator i); 

iterator find(const Predicate& predicate(const T element));//line 47 
void sort(const Compare& comparer());//line 48 

int getSize() const; 
iterator begin(); 
iterator end(); 
~List(); 
}; 

/*Implimentation of List functions*/ 
template <class T> 
List<T>::List(const List & l) { 
first = 0; 
last = 0; 
size = 0; 
for (Node<T> * current = l.first; current != 0; current = current -> next){ 
    pushBack(current -> data); 
} 
} 

template <typename T> 
void List<T>::pushBack(T element){ 
Node<T>* newnode = new Node<T>(element); 
if (newnode->prev == NULL) { 
    first = newnode; 
    last = newnode; 
}else{ 
    newnode->prev = last; 
    last->next = newnode; 
    last = newnode; 
} 
} 

template <typename T> 
void List<T>::insert(const T& element, iterator i){ 
if (i.position == NULL){ 
    pushBack(element); 
    ++size; 
    return; 
} 
Node<T>* after = i.position; 
Node<T>* before = after->prev; 
Node<T>* newnode = new Node<T>(element); 
newnode->prev = before; 
newnode->next = after; 
after->prev = newnode; 
if (before == NULL) { 
    first = newnode; 
} 
else{ 
    before->next = newnode; 
} 
++size; 
} 

template <typename T> 
typename List<T>::iterator List<T>::remove(iterator iter){ 
    if(iter.position != NULL){ 
     Node<T>* remove = iter.position; 
     Node<T>* before = remove->prev; 
     Node<T>* after = remove->next; 
     if (remove == first){ 
      first = after; 
     } else{ 
      before->next = after; 
     } 
     if (remove == last){ 
      last = before; 
     }else{ 
      after->prev = before; 
     } 
     iter.position = after; 
     --size; 
     delete remove; 
     return iter; 
    }else{ 
    throw ElementNotFound(); 
    } 
} 

template <typename T> 
typename List<T>::iterator List<T>::begin(){ 
return iterator(first); 
} 

template <typename T> 
typename List<T>::iterator List<T>::end(){ 
if(size != 0){ 
    return iterator(last); 
} 
throw ElementNotFound(); 
} 

//TODO 
template <typename T> 
template <typename Predicate> 
typename List<T>::iterator List<T>::find(const Predicate& predicate(const T element)) {//line145 
iterator iter; 
Node<T>* current = iter.position; 
Node<T>* end = iter.last; 
for(; current != end; ++current){ 
    if(predicate(*current)){ 
     return current; 
    } 
} 
return end; 
} 

//TODO 
template <typename Compare> 
template <typename T> 
void List<T>::sort(const Compare& comparer()){//line154 
Iterator<T> current = position; 
Iterator<T> end = last; 
for(; current != end; ++current){ 
    if(comparer(*current , *current+1) != true){ 

    } 
} 
} 

template <typename T> 
int List<T>::getSize() const{ 
return size; 
} 

template <class T> 
List <T>::~List() { 
Node <T> * firstNode = first; 
while (firstNode != 0)  { 
    Node <T> * nextNode = firstNode->next; 
    delete firstNode; 
    firstNode = nextNode; 
} 
} 

/*Node Class*/ 
template <typename T> class Node { 
private: 
T data; 
Node* next; 
Node* prev; 
friend class List<T>; 
friend class Iterator<T>; 
public: 
Node(T element){ 
    data = element; 
    prev = NULL; 
    next = NULL; 
} 
~Node(){} 
}; 

/*Iterator class*/ 
template <typename T> class Iterator{ 
private: 
Node<T>* position; 
Node<T>* last; 
friend class List<T>; 
public: 
Iterator(){ 
    position = NULL; 
    last = NULL; 
} 
Iterator(Node<T> * source): position(source) { } 
T& getElement()const; 
bool operator==(Iterator b) const; 
bool operator!=(Iterator b) const; 
T & operator*(); 
Iterator & operator++(); 
Iterator & operator++(int); 
~Iterator(){} 
}; 

/*Implimentation of Iterator class function*/ 
template <class T> 
T& Iterator<T>::getElement() const{ 
if(position != NULL){ 
    return position->data; 
} 
else{ 
    throw ElementNotFound(); 
} 
} 

template <typename T> 
bool Iterator<T>::operator==(Iterator b) const{ 
return position == b.position; 
} 

template <typename T> 
bool Iterator<T>::operator!=(Iterator b) const{ 
return position != b.position; 
} 

template <typename T> 
    T & Iterator<T>::operator*() { 
return position->data; 
} 

template <class T> 
Iterator<T> & Iterator<T>::operator++() { 
position = position->next; 
return *this; 
} 

template <class T> 
Iterator<T> & Iterator<T>::operator++(int){ 
position = position->next; 
return *this; 
} 

,我收到的消息是:

C:/Programs/eclipse-with-gcc/eclipse/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:161: error: `::swprintf' has not been declared 
C:/Programs/eclipse-with-gcc/eclipse/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:168: error: `::vswprintf' has not been declared 

..\list.h:140: error: expected constructor, destructor, or type conversion before "List" 
..\list.h:154: error: no member function `sort' declared in `List<T>' 
..\list.h:154: error: invalid function declaration 
+1

如果您只是放出問題部分,這可能會產生您收到的錯誤將是有益的。沒有人會瀏覽你的所有代碼。 – DumbCoder

+1

可怕的格式化是什麼? –

+0

你不可能快速獲得那些不可讀的代碼的幫助。清理它看起來像真正的代碼。 –

回答

0

看起來像你對我依賴的名字(List<T>::iterator,尤其是),並因此need to use typename

+0

它仍然不好 .. \ list.h:140:錯誤:原型爲'typename List :: iterator List :: find(const Predicate&(*)(T))'不匹配類'List ' .. \ list.h:47:error:candidate is:Iterator List :: find(const Predicate&(*)(T)) .. \ list.h:140:error:template definition of non -template'typename List :: iterator List :: find(const Predicate&(*)(T))' .. \ list.h:154:error:no'list'' .. \ list.h:154:錯誤:無效函數聲明 – user808176

+0

@ user808176:我認爲這是一個新問題。但是我不會篩選你格式不好的代碼(你的縮進在哪裏?你怎麼讀這個?!)來幫助你一次解決一個編譯錯誤。 –

0
template <typename T> 
template <typename Predicate> 
List<T>::iterator List<T>::find(const Predicate& predicate(const T element)) { 

template <typename Compare> 
template <typename T> 
void List<T>::sort(const Compare& comparer()) { 

它看起來像你正在聲明函數參數預期的某種功能。嘗試

List<T>::find(const Predicate& predicate) 
List<T>::sort(const Compare& comparer) 
+0

我已經做到了。這並沒有解決問題。編譯器要求構造函數或分析器 – user808176

+0

因此,包含Predicate和Compare類定義的頭文件。 – danadam

+0

完成 - 相同的結果。 – user808176