1

我无法理解在 C++ 中使用嵌套类的概念。

我正在尝试为我的链表创建一个迭代器。我已将其声明为 LinkedList.h 私有范围内的嵌套类

但是我在哪里定义迭代器类的函数呢?现在,我正试图在文件的底部以及 LinkedList 的所有函数中这样做。但是无论我如何尝试扭曲和旋转范围定义,我都无法让它工作。我应该以不同的方式做这件事吗?

迭代器类

class Iterator {

    private:

        LinkedList<T>* l;
        Node<T>* node;

    public:

        Iterator(LinkedList<T> *ll, Node<T> *n);        
        T get();
        int hasNext();
        void moveNext();

    };

四个函数定义

template <typename T>
typename LinkedList<T>::Iterator<T>::Iterator(LinkedList<T> *ll, Node<T> *n) {
    l = ll;
    node = n;
}

template <typename T>
T LinkedList<T>::Iterator<T>::get() {
    return node->getData();
}

template <typename T>
int LinkedList<T>::Iterator<T>::hasNext() {
    return node->getNext();
}

template <typename T>
void LinkedList<T>::Iterator<T>::moveNext() {
    node = node->getNext();
}

我得到的错误是:

“使用未定义类型:LinkedList::Iterator”

“语法错误:意外类型 LinkedList”

“语法错误:缺少';' 前 '{'”

还有很多。

我非常非常失落。

编辑:

完整的 LinkedList.h 文件:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
#include "List.h"
#include <iostream>

template <typename T>
class LinkedList : public List {

private:

    int n;
    Node<T> *first;
    Node<T> *last;

    template <typename T>
    class Iterator {

    private:

        LinkedList<T>* l;
        Node<T>* node;

    public:

        Iterator(LinkedList<T> *ll, Node<T> *n);        
        T get();
        int hasNext();
        void moveNext();

    };

public:

    LinkedList();
    LinkedList(const LinkedList & ll);
    ~LinkedList();
    int size();
    void clear();
    void addFirst(T data);
    void addLast(T data);
    T removeFirst();
    T removeLast(); 
    T getFirst();
    T getLast();
    Node<T>* getFirstNode() const;
    void addAt(int pos, T data);
    T removeAt(int pos);
    T getAt(int pos);   
    LinkedList& operator=(const LinkedList<T> &right);
    T operator[](int i);
    LinkedList& operator+(const LinkedList<T> &right);
    LinkedList& operator+=(const LinkedList<T> &right);
    friend std::ostream& operator<<(std::ostream &os, const LinkedList<T> & ll);
    typename LinkedList<T>::Iterator<T> getIterator();
    void mergeSort();   
};

template <typename T>
LinkedList<T>::LinkedList() {
        this->n = 0;
        this->first = 0;
        this->last = 0;
    }

template <typename T>
LinkedList<T>::LinkedList(const LinkedList & ll) {
    this-> n = 0;
    this-> first = 0;
    this-> last = 0;

    Node *temp = ll.first;

    while(temp) {
        addLast(temp->getData());
        temp = temp->getNext();
    }

}

template <typename T>
void LinkedList<T>::addFirst(T data) {
    Node<T> *p = new Node<T>(data, first);
    first = p;
    if(!n)
        last = p;
    n++;
}

template <typename T>
void LinkedList<T>::addLast(T data) {
    Node<T> *p = new Node<T>(data, 0);
    if(!n)
        first = last = p;
    else {
        last->next = p;
        last = p;
    }
    n++;
}

template <typename T>
T LinkedList<T>::removeFirst() {
    T a = 0;
    if(!n)
        throw "Can't retrieve element from empty list!";
    a = first->getData();
    Node<T> *p = first->next;
    delete first;
    first = p;
    n--;
    return a;
}

template <typename T>
T LinkedList<T>::removeLast() {
    T a = 0;
    if(!n)
        throw "Can't retrieve element from empty list!";
    if(n == 1) {
        a = last->getData();
        delete first;
        first = last = 0;
    }
    else {
        a = last->getData();
        Node<T> *p = first;
        while(p->next->next != 0)
            p = p->next;
        delete p->next;
        p->next = 0;
        last = p;
    }
    n--;
    return a;
}

template <typename T>
T LinkedList<T>::getFirst() {
    if(n < 1)
        throw "Can't retrieve element from empty list!";
    return first->getData();
}

template <typename T>
T LinkedList<T>::getLast() {
    if(n < 1)
        throw "Can't retrieve element from empty list!";
    return last->getData();
}

template <typename T>
Node<T>* LinkedList<T>::getFirstNode() const {
    return first;
}

template <typename T>
int LinkedList<T>::size() {
    return n;
}

template <typename T>
T LinkedList<T>::getAt(int pos) {
    if(pos >= n)
        throw "Element index out of bounds!";       
    Node<T> *temp = first;
    while(pos > 0) {
        temp = temp->next;
        pos--;
    }
    return temp->getData();     
}

template <typename T>
void LinkedList<T>::clear() {
    Node<T> *current = first;
    while(current) {
        Node<T> *next = current->next;
        delete current;
        if(next)
            current = next;
        else
            current = 0;
    }
}

template <typename T>
void LinkedList<T>::addAt(int pos, T data) {
    if(pos >= n)
        throw "Element index out of bounds!";       
    if(pos == 0)
        addFirst(data);
    else {
        Node<T> *temp = first;
        while(pos > 1) {
            temp = temp->next;
            pos--;
        }
        Node<T> *p = new Node<T>(data, temp->next);
        temp-> next = p;
        n++;
    }
}

template <typename T>
T LinkedList<T>::removeAt(int pos) {
    if(pos >= n)
        throw "Element index out of bounds!";       
    if(pos == 0)
        return removeFirst();
    if(pos == n - 1)
        return removeLast();
    else {
        Node<T> *p = first;
        while(pos > 1) {
            p = p->next;
            pos--;
        }
        T a = p->next->getData();
        Node<T> *temp = p->next;
        p->next = p->next->next;
        delete temp;
        n--;
        return a;
    }       
}

template <typename T>
LinkedList<T>::~LinkedList() {
    clear();
}

template <typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T> &right) {
    if(this != &right) {

        n = 0;
        first = 0;
        last = 0;

        Node<T> *temp = right.first;
        while(temp) {
            addLast(temp->getData());
            temp = temp->getNext();
        }           
    }
    return *this;
}

template <typename T>
T LinkedList<T>::operator[](int i) {
    return getAt(i);
}

template <typename T>
LinkedList<T>& LinkedList<T>::operator+(const LinkedList<T> &right) {
    Node<T> *temp = right.first;
    while(temp) {
        addLast(temp->getData());
        temp = temp->getNext();
    }
    return *this;
}

template <typename T>
LinkedList<T>& LinkedList<T>::operator+=(const LinkedList<T> &right) {
    Node<T> *temp = right.first;
    while(temp) {
        addLast(temp->getData());
        temp = temp->getNext();
    }
    return *this;
}

template <typename T>
std::ostream& operator<<(std::ostream &os, const LinkedList<T> &ll) {
    Node<T> *temp = ll.getFirstNode();
    while(temp) {
        os<<temp->getData()<<std::endl;
        temp = temp->getNext();
    }
    return os;
}

template <typename T>
typename LinkedList<T>::Iterator<T>::Iterator(LinkedList<T> *ll, Node<T> *n) {
    l = ll;
    node = n;
}

template <typename T>
T LinkedList<T>::Iterator<T>::get() {
    return node->getData();
}

template <typename T>
int LinkedList<T>::Iterator<T>::hasNext() {
    return node->getNext();
}

template <typename T>
void LinkedList<T>::Iterator<T>::moveNext() {
    node = node->getNext();
}

template <typename T>
typename LinkedList<T>::Iterator<T> LinkedList<T>::getIterator() {
    return new Iterator<T>(*this, *first);
}

#endif
4

2 回答 2

2

您要么需要使用 LinkedList 类的模板类型名(这是有道理的)。这意味着 Iterator 类不需要单独的模板类型名。

template <typename T>
class Iterator {

应该只是

class Iterator {

然后成员将被定义为

template <typename T>
T LinkedList<T>::Iterator::get() {

或者如果你真的需要一个不同的迭代器类型名。然后至少你必须更改名称,T因为它会影响LinkedList. 就像是

template <typename V>
class Iterator {
    ...
    V get();
    ...

成员声明将采用以下形式

template <typename T>
template <typename V>
V LinkedList<T>::Iterator<V>::get() {
于 2013-03-30T13:24:21.003 回答
1

我认为这会做到:

template <typename T>
LinkedList<T>::Iterator::Iterator(LinkedList<T> *ll, Node<T> *n) {
  l = ll;
  node = n;
}

template <typename T>
T LinkedList<T>::Iterator::get() {
  return node->getData();
}

template <typename T>
int LinkedList<T>::Iterator::hasNext() {
  return node->getNext();
}

template <typename T>
void LinkedList<T>::Iterator::moveNext()
{
  node = node->getNext();
}
于 2013-03-30T13:06:19.307 回答