c ++ - 在哪里定义嵌套类的函数?

时间:2013-03-30 12:47:27

标签: c++ scope nested

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

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

但是我在哪里定义类Iterator的功能?现在,我试图在文件的底部以及所有LinkedList的功能中这样做。但无论我如何扭曲和旋转范围定义,我都无法使其发挥作用。我应该以不同的方式做这件事吗?

Iterator类

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();
}

我得到的错误是:

&#34;使用未定义的类型:LinkedList :: Iterator&#34;

&#34;语法错误:意外类型LinkedList&#34;

&#34;语法错误:缺少&#39;;&#39;之前&#39; {&#39;&#34;

还有更多。

我非常非常失落。

编辑:

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

2 个答案:

答案 0 :(得分:2)

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

template <typename T>
class Iterator {

应该只是

class Iterator {

然后成员将被定义为

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

如果你真的需要Iterator的不同类型名称,请

OR 。然后,至少您必须更改T的名称,因为它会影响LinkedList的名称。像

这样的东西
template <typename V>
class Iterator {
    ...
    V get();
    ...

memeber声明将采用

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

答案 1 :(得分: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();
}