2012-05-05 2 views
1

내가 반복자 내 자신의 템플릿 목록 클래스를 작성하고,이 오류가있어 ((( 컴파일러 :는 MinGW에서 GCC (내가 사용하는 이클립스 IDE) 내 코드 : CPP :예상 생성자, 소멸자, 또는 형식 변환

#include <iostream> 
#include "tlist.h" 
using namespace std; 
int main(){ 
    return 0; 
} 
헤더

template<typename T> 
class element{ 
private: 
    element<T>* pre; 
    element<T>* next; 
    T data; 
public: 
    element(){}; 
    ~element(){}; 
    element(T d){ 
     pre=NULL; 
     next=NULL; 
     data=d; 
    } 
    void SetPre(element<T>* a){ 
     pre=a; 
    }; 
    void SetNext(element<T>* a){ 
     next=a; 
    }; 
    void SetData(T d){ 
     data=d; 
    } 
    element<T>* GetPre(){ 
     return pre; 
    } 
    element<T>* GetNext(){ 
     return next; 
    } 
    T GetData(){ 
     return data; 
    } 
}; 
template<typename T> 
class Tlist{ 
private: 
    element<T>* first; 
    element<T>* last; 
    int size; 
public: 
     int GetSize(){ 
     return size; 
    } 
     Tlist(){ 
     first=NULL; 
     last=NULL; 
     size=0; 
    } 
     ~Tlist(){}; 
     Tlist(T d){ 
     element<T>* tmp; 
     tmp->SetPre(NULL); 
     tmp->SetNext(NULL); 
     tmp->SetData(d); 
     this->size=1; 
     this->first=tmp; 
     this->last=tmp; 
       } 
    class Iterator{ 
    private: 
     element<T>* pointing; 
    public: 
     Iterator(){ 
      pointing=NULL; 
     }; 
     ~Iterator(){}; 
     Tlist<T>::Iterator& operator++(int i); 
     element<T>* operator*(); 
     Iterator(element<T>* b){ 
      pointing=b; 
     } 
    }; 
    bool empty(); 
    void clear(); 
    void swap(Tlist<T>& lst); 
    Tlist<T>::Iterator begin(); 
    Tlist<T>::Iterator end(); 
    T front(); 
    T back(); 
    void push_back(T t); 
    void push_front(T t); 
    void pop_front(); 
    void pop_back(); 
    Tlist<T>::Iterator insert(Tlist<T>::Iterator pos,T data); 
    Tlist<T>::Iterator erase(Tlist<T>::Iterator pos); 
    void splice(Tlist<T>::Iterator pos,Tlist<T>* in); 
    Tlist<T> operator=(Tlist<T> const &other){ 
    this->size=other.size; 
    this->first=other.first; 
    this->last=other.last; 
    return *this; 
}; 
}; 
template<typename T> 
Tlist<T>::Iterator& Tlist<T>::Iterator::operator++(int i){ 
    element<T>* temp=*this; 
    if(temp->GetNext()!=NULL) this->pointing=temp->GetNext(); 
    return *this; 
} 
template<typename T> 
element<T>* Tlist<T>::Iterator::operator*(){ 
    return pointing; 
} 
template<typename T> 
bool Tlist<T>::empty(){ 
      if(this->size==0) return true; 
      else return false; 
}; 
template<typename T> 
void Tlist<T>::clear(){ 
      element<T>* son; 
      element<T>* temp; 
      son=this->first; 
      while(son!=NULL){ 
       temp=son->next; 
       delete son; 
       son=temp; 
      } 
      this->size=0; 
}; 
template<typename T> 
void Tlist<T>::swap(Tlist<T>& lst){ 
      int temp=this->size; 
      this->size=lst->size; 
      lst->size=temp; 
      Tlist<T>* tmp; 
      *tmp=*this; 
      *this=lst; 
      lst=*tmp; 
} 
template<typename T> 
Tlist<T>::Iterator Tlist<T>::begin(){ 
       Tlist<T>::Iterator res(this->first); 
       return res; 
}; 
template<typename T> 
Tlist<T>::Iterator Tlist<T>::end(){ 
       Tlist<T>::Iterator res(this->last); 
       return res; 
} 
template<typename T> 
T Tlist<T>::front(){ 
return this->first->GetData(); 
} 
template<typename T> 
T Tlist<T>::back(){ 
    return this->last->GetData(); 
}; 
template<typename T> 
void Tlist<T>::push_front(T d){ 
    element<T>* temp(d); 
    this->size++; 
    this->first->SetPre(temp); 
    temp->SetNext(this->first); 
    this->first=temp; 
} 
template<typename T> 
void Tlist<T>::push_back(T d){ 
    element<T>* temp(d); 
    this->last->SetNext(temp); 
    this->size++; 
    temp->SetPre(this->last); 
    this->last=temp; 
} 
template<typename T> 
void Tlist<T>::pop_front(){ 
    element<T>* temp=this->first->GetNext; 
    delete this->first; 
    this->first=temp; 
    this->size--; 
} 
template<typename T> 
void Tlist<T>::pop_back(){ 
    element<T>* temp=this->last; 
    delete this->last; 
    this->last=temp; 
    this->size--; 
} 
template<typename T> 
Tlist<T>::Iterator Tlist<T>::insert(Tlist<T>::Iterator pos,T d){ 
    element<T>* temp(d); 
    element<T>* p=*pos; 
    element<T>* n=p->GetNext(); 
    p->SetNext(temp); 
    temp->SetPre(p); 
    if(n!=NULL){ 
     n->SetPre(temp); 
     temp->SetNext(n); 
    } 
    this->size++; 
    return pos++; 
} 
template<typename T> 
Tlist<T>::Iterator Tlist<T>::erase(Tlist<T>::Iterator pos){ 
    if(pos==this->end()){ 
        this->pop_back(); 
        return this->end(); 
       } 
    if(pos==this->begin()){ 
     this->pop_front(); 
     return this->begin(); 
    } 
    else{ 
     element<T>* del=*pos; 
     element<T>* p=del->GetPre(); 
     element<T>* n=del->GetNext(); 
     pos++; 
     p->SetNext(n); 
     n->SetPre(p); 
     delete del; 
     this->size--; 
     return pos; 
    } 
}; 
template<typename T> 
void Tlist<T>::splice(Tlist<T>::Iterator pos,Tlist<T>* a){ 
    this->size+=a->GetSize(); 
    element<T>* p=*pos; 
    element<T>* n=p->GetNext(); 
    p->SetNext(a->first); 
    a->first->SetPre(p); 
    n->SetPre(a->last); 
    a->last->SetNext(n); 
    a->size=0; 
    a->first=NULL; 
    a->last=NULL; 
} 

및 오류는 다음과 같습니다

..\tlist.h:99: error: expected constructor, destructor, or type conversion before '&' token 
..\tlist.h:136: error: expected constructor, destructor, or type conversion before 'Tlist' 
..\tlist.h:141: error: expected constructor, destructor, or type conversion before 'Tlist' 
..\tlist.h:184: error: expected constructor, destructor, or type conversion before 'Tlist' 
..\tlist.h:198: error: expected constructor, destructor, or type conversion before 'Tlist' 
당신이 할 수있는 경우 컴파일러는 먼저 anything라는 클래스 Tlist<T>의 일부 정적 변수를 의미 할 수 있다고 생각

Tlist<T>::anything 

을 말할 때3210

, 나를

+1

(편집 그냥 typename상의 article에 부딪쳤다) 그리고'TList' 클래스 다음에 여분의'};가 있다고 생각합니다.하지만 쉽게 알 수는 없습니다. – chris

+0

하지만 모든 erorrs는 iterator 클래스와 연결되어 있습니다. 문제가 있다고 생각합니다 ... – user44532

+0

'Tlist :: Iterator &'전에'typename'이 필요합니다. 그것은 첫 번째 문제를 해결해야합니다. 다른 많은 장소들도 마찬가지입니다. – chris

답변

2

을 도와 줘요. 그것을 이야기하기 위해 당신이 typename과 구조를 앞에 추가해야하는 유형을 나타냅니다

template<typename T> 
typename Tlist<T>::Iterator& TList<T>::Iterator::operator++(int){ 
... 

: 당신은 당신의 기능의 절반 후 세미콜론을 넣어

+1

이것은 유일한 오류입니다 (여분의 세미콜론과 후위 연산자의 반환 유형 제외). GCC는 다음과 같이 정말 도움이됩니다 :'| 197 | 오류 : 'Tlist :: Iterator'전에 'typename'이 필요합니다. 'Tlist '이 종속 범위이기 때문에 | ' – chris

+0

thx 많이 들었습니다! – user44532

관련 문제