2012-02-08 10 views
1

私は非常に混乱しています。あなたが与えることができるすべての助けに感謝します。私はテンプレートクラスのこのコードを書く方法が不明です。私はこのようにいくつかの奇妙なコンパイル・エラー取得維持:最も重要なC++のテンプレートクラス - リンクリストの作成に問題があります

g++ -c test_list.cpp 
List.cpp: In constructor ‘cop4530::List<T>::iterator::iterator(cop4530::List<T>::Node*) [with T = int]’: 
List.cpp:247: instantiated from ‘typename cop4530::List<T>::iterator cop4530::List<T>::begin() [with T = int]’ 
test_list.cpp:25: instantiated from here 
List.cpp:112: error: invalid conversion from ‘cop4530::List<int>::Node*’ to ‘int’ 
List.cpp:112: error: initializing argument 1 of ‘cop4530::List<T>::Node::Node(const T&, cop4530::List<T>::Node*, cop4530::List<T>::Node*) [with T = int]’ 
List.cpp: In constructor ‘cop4530::List<T>::iterator::iterator(cop4530::List<T>::Node*) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’: 
List.cpp:247: instantiated from ‘typename cop4530::List<T>::iterator cop4530::List<T>::begin() [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’ 
test_list.cpp:134: instantiated from here 
List.cpp:112: error: no match for ‘operator=’ in ‘*((cop4530::List<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::iterator*)this)->cop4530::List<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::iterator::<anonymous>.cop4530::List<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::const_iterator::current = p’ 
List.h:11: note: candidates are: cop4530::List<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Node& cop4530::List<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Node::operator=(const cop4530::List<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Node&) 
make: *** [test_list.o] Error 1 

を - ここに私が修正しようとしている正確なコードである:ここでは

template <class T> 
    typename List<T>::iterator List<T>::begin() 
    { // iterator to first element 
     if (!empty()) 
     return iterator(head->next); 
    } 

はフルヘッダーです:

#ifndef DL_LIST_H 
#define DL_LIST_H 
#include <iostream> 

namespace cop4530 { 

template <typename T> 
class List { 
private: 
    // nested Node class 
    struct Node { 
    T data; 
    Node *prev; 
    Node *next; 

    Node(const T & d = T(), Node *p = NULL, Node *n = NULL) 
     : data(d), prev(p), next(n) {} 
    }; 

public: 
    //nested const_iterator class 
    class const_iterator { 
    public: 
    const_iterator(); // default zero parameter constructor 
    const T & operator*() const; // operator*() to return element 

    // increment/decrement operators 
    const_iterator & operator++(); 
    const_iterator operator++(int); 
    const_iterator & operator--(); 
    const_iterator operator--(int); 

    // comparison operators 
    bool operator==(const const_iterator &rhs) const; 
    bool operator!=(const const_iterator &rhs) const; 

    protected: 
    Node *current; // pointer to node in List 
    T & retrieve() const; // retrieve the element refers to 
    const_iterator(Node *p); // protected constructor 

    friend class List<T>; 
    }; 

    // nested iterator class 
    class iterator : public const_iterator { 
    public: 
    iterator() {} 
    T & operator*(); 
    const T & operator*() const; 

    // increment/decrement operators 
    iterator & operator++(); 
    iterator operator++(int); 
    iterator & operator--(); 
    iterator operator--(int); 

    protected: 
    iterator(Node *p); 
    friend class List<T>; 
    }; 

public: 
    // constructor, desctructor, copy constructor 
    List(); // default zero parameter constructor 
    List(const List &rhs); // copy constructor 
    // num elements with value of val 
    explicit List(int num, const T& val = T()); 
    // constructs with elements [start, end) 
    List(const_iterator start, const_iterator end); 

    ~List(); // destructor 

    // assignment operator 
    const List& operator=(const List &rhs); 

    // member functions 
    int size() const; // number of elements 
    bool empty() const; // check if list is empty 
    void clear(); // delete all elements 
    void reverse(); // reverse the order of the elements 

    T &front(); // reference to the first element 
    const T& front() const; 
    T &back(); // reference to the last element 
    const T & back() const; 

    void push_front(const T & val); // insert to the beginning 
    void push_back(const T & val); // insert to the end 
    void pop_front(); // delete first element 
    void pop_back(); // delete last element 

    void remove(const T &val); // remove all elements with value = val 

    // print out all elements. ofc is deliminitor 
    void print(std::ostream& os, char ofc = ' ') const; 

    iterator begin(); // iterator to first element 
    const_iterator begin() const; 
    iterator end(); // end marker iterator 
    const_iterator end() const; 
    iterator insert(iterator itr, const T& val); // insert val ahead of itr 
    iterator erase(iterator itr); // erase one element 
    iterator erase(iterator start, iterator end); // erase [start, end) 


private: 
    int theSize; // number of elements 
    Node *head; // head node 
    Node *tail; // tail node 

    void init(); // initialization 
}; 

// overloading comparison operators 
template <typename T> 
bool operator==(const List<T> & lhs, const List<T> &rhs); 

template <typename T> 
bool operator!=(const List<T> & lhs, const List<T> &rhs); 

// overloading output operator 
template <typename T> 
std::ostream & operator<<(std::ostream &os, const List<T> &l); 

// include the implementation file here 
#include "List.cpp" 

} // end of namespace 4530 

#endif 
ここで

.cppファイルの中で最もです:

using namespace std; 

// --------------------- CONST_ITERATOR --------------------- // 
template <class T> 
List<T>::const_iterator::const_iterator() 
{ // default zero-parameter constructor. Set pointer current to NULL. 
    current = NULL; 
} 

template <class T> 
const T& List<T>::const_iterator::operator*() const 
{ // returns a reference to the corresponding element in the list by calling retrieve() member function. 
    return retrieve(); 
} 

template <class T> 
typename List<T>::const_iterator& List<T>::const_iterator::operator++() 
{ 
    current = current->next; 
    return *this; 
} 
template <class T> 
typename List<T>::const_iterator List<T>::const_iterator::operator++(int) 
{ 
    const_iterator old = *this; 
    ++(*this); 
    return old; 
} 
template <class T> 
typename List<T>::const_iterator& List<T>::const_iterator::operator--() 
{ 
    current = current->prev; 
    return *this; 
} 
template <class T> 
typename List<T>::const_iterator List<T>::const_iterator::operator--(int) 
{ 
    const_iterator old = *this; 
    --(*this); 
    return old; 
} 

template <class T> 
bool List<T>::const_iterator::operator==(const const_iterator &rhs) const 
    {return current == rhs.current;} 

template <class T> 
bool List<T>::const_iterator::operator!=(const const_iterator &rhs) const 
    {return !(*this == rhs);} 

template <class T> 
T& List<T>::const_iterator::retrieve() const 
{ // return a reference to the corresponding element in the list. 
    return current->data; 
} 

template <class T> 
List<T>::const_iterator::const_iterator(Node *p) 
{ // one-parameter constructor 
    // Set pointer current to the given node pointer p. 
    current = p; 
} 

// --------------------- ITERATOR --------------------- // 

template <typename T> 
T& List<T>::iterator::operator*() 
{ 
    return List<T>::iterator::retrieve(); 
} 

template <typename T> 
const T& List<T>::iterator::operator*() const 
{ 
    return List<T>::iterator::retrieve(); 
} 

template <class T> 
typename List<T>::iterator& List<T>::iterator::operator++() 
{ 
    *this->current = *this->current->next; 
    return *this; 
} 

template <class T> 
typename List<T>::iterator List<T>::iterator::operator++(int) 
{ 
    iterator old = *this; 
    ++(*this); 
    return old; 
} 

template <class T> 
typename List<T>::iterator& List<T>::iterator::operator--() 
{ 
    *this->current = *this->current->prev; 
    return *this; 
} 

template <class T> 
typename List<T>::iterator List<T>::iterator::operator--(int) 
{ 
    iterator old = *this; 
    --(*this); 
    return old; 
} 

template <class T> 
List<T>::iterator::iterator(Node *p) 
{ // one-parameter constructor 
    // Set current to the given node pointer p 
    *this->current = p; 
} 

// --------------------- LIST --------------------- // 

template <class T> 
List<T>::List() 
{ init(); } 

template <class T> 
List<T>::List(const List & rhs) 
{ // Copy constructor 
    init(); 
    *this = rhs; 
} 

template <class T> 
List<T>::List(int num, const T& val) 
{ //Constructs a list with num elements, all initialized with value val 
    init(); 
    iterator itr = begin(); 
    for (int i = 0; i < num; ++i) 
    { 
    insert(itr, val); 
    ++itr; 
    } 
} 

template <class T> 
List<T>::~List() 
{ // Destructor 
    clear(); 
    delete head; 
    delete tail; 
} 

template <class T> 
const typename List<T>::List& List<T>::operator=(const List &rhs) 
{ // Assignment operator 
    iterator ritr = rhs.first(); 
    iterator itr = begin(); 

    if(this != &rhs) 
    { 
     clear(); 
     for(; ritr != NULL; ritr++, itr++) 
     insert(ritr.retrieve(), itr); 
    } 
    return *this; 
} 

template <class T> 
int List<T>::size() const 
{ // return the number of elements in the List 
    return theSize; 
} 

template <class T> 
bool List<T>::empty() const 
{ // check if list is empty 
    return head->next == NULL; 
} 

template <class T> 
void List<T>::clear() 
{ // delete all elements 
    while(!empty()) 
    erase(begin()); 
} 

template <class T> 
T& List<T>::front() 
{ // reference to the first element 
    return head->next->data; 
} 

template <class T> 
typename List<T>::iterator List<T>::begin() 
{ // iterator to first element 
    if (!empty()) 
    return iterator(head->next); 
} 

template <class T> 
typename List<T>::iterator List<T>::end() 
{ // end marker iterator 
    if (!empty()) 
    return iterator(tail->prev); 
} 

template <class T> 
typename List<T>::iterator List<T>::insert(iterator itr, const T & x) 
{ 
    Node *p = itr.current; 
    theSize++; 
    return iterator(p->prev = p->prev->next = new Node(x, p->prev, p)); 
} 

template <class T> 
typename List<T>::iterator List<T>::erase(iterator itr) 
{ 
    Node *p = itr.current; 
    iterator retVal(p->next); 
    p->prev->next = p->next; 
    p->next->prev = p->prev; 
    delete p; 
    theSize--; 

    return retVal; 
} 

template <class T> 
typename List<T>::iterator List<T>::erase(iterator start, iterator end) 
{ 
    for(iterator itr = start; itr != end;) 
    itr = erase(itr); 

    return end; 
} 

template <class T> 
void List<T>::init() 
{ // Initaialize the member variables of a List 
    theSize = 0; 
    head = new Node; 
    tail = new Node; 
    head->next = tail; 
    tail->prev = head; 
} 

最初に教えてあげましょう(a)クラステンプレートの大きなファイルである別々のファイルであることは分かっていますが、この割り当てでは変更できません。 (b)これは宿題のためのものです。 (c)私は同じコードに沿って多かれ少なかれコンパイルエラーを取り出すこのコードのいくつかのバリエーションを試しました。あなたが私を助けてくれたら、私はとても幸せになれるでしょう...これを1時間か2時間受けました。

ありがとうございます!それ以上の情報が必要な場合は、私に教えてください!

答えて

1

これはあなたの問題です:

return iterator itr(head->next); 

あなたはおそらくこれを実行する必要があります。

return iterator(head->next); 

コードをあなたは声明である、itrという名前iteratorを宣言しようとしている持っているように、式ではありません(returnが必要です)。

+0

これは私が元々持っていたものです。今は新しいコンパイラ・エラーが発生します。私は新しいコンパイラエラーを反映するために上記の投稿を編集しました(そして、この変更をコードに追加しました)。これを修正できるものは何ですか? – jordaninternets

0

私はあなたと同じコンパイラを使用していませんが、私は、他の方法ではなく、cppにヘッダーを追加し、cppの名前空間に 'using'を追加し、operator =の実装の関数シグネチャを修正し、ビルドするために与えたものを得ました。

関連する問題