如何在hpp文件和cpp文件中正确使用嵌套类?

我试图用迭代器实现一个单链表,作为链表类的嵌套类。我遇到了很多错误,这些错误与使用迭代器有关。我花了几个小时研究这个问题,但不幸的是,许多解决方案都包含模板,但我无法使用它们。每当我解决一个问题时,就会出现一个新问题。

我的cpp文件中出现以下错误:

  • 使用未声明的标识符“当前”
  • 预期为';'在顶级声明符之后(我很困惑为什么要得到它,因为我认为我在hpp文件中声明了迭代器
  • 重载的“ operator ++”必须至少具有一个类或枚举类型的参数(在这种情况下,我们不应该为重载的运算符提供参数,对吧?我只想进行迭代,以便现在指向链表中的下一个项目
  • 在非静态成员函数外部无效使用“ this”
  • “迭代器”不是类,名称空间或枚举

如果我发现自己的代码有什么问题,我将继续做更多的研究并在这里报告,以期在其他人有相同问题的情况下为您提供帮助。

非常感谢!

这是我的hpp文件:

#ifndef linkedlist_h
#define linkedlist_h


  class LinkedList {
  public:


    struct Node {
      //Node(Node *aNext=nullptr) : next(aNext) {}
        int  value;   //this is the value you want to save
        Node *next;  //this points to the next node in the list (or nullptr)
    };
    //friend class Iterator; //do Ineed this even though it's a nested class

    Node *root;

    //---------------------------------------------------------------

    //add a nesTED Interator class...
    class Iterator {
    public:
        Iterator(); //default constructor
        Iterator(Node* aNode);//constructor
        ~Iterator(); //dtor
        Iterator operator++();
        Iterator operator++(int);
        bool operator==(const Iterator &anIterator);
        bool operator!=(const Iterator &anIterator);
        int operator*();
        operator Node*();
        Node *current; //do I need to put LinkedList since it's a nested class?

      //add all the necessary operators

    protected:
    };

    //--------------------------------------------------------------

    LinkedList(); //default constructor...
    LinkedList(const LinkedList& aCopy); //copy ctor
    ~LinkedList(); //dtor
    LinkedList& operator=(const LinkedList& aCopy); //assignment operator

    void append(int value);
    void prepend(int value);
    void remove(int value);
    int size(); //needs to return unsigned int????
    Iterator begin();
    Iterator end();
    Iterator find(int value);


  protected:
  };




#endif /* linkedlist_h */

这是我的cpp文件:

#include <stdio.h>
#include "LinkedList.hpp"


//class Iterator;
  LinkedList::LinkedList() {
      root=nullptr;
  }

LinkedList::LinkedList(const LinkedList& aCopy){ //copy ctor
    Node *temp=aCopy.root;
    Node *newNode = new Node;
    root=newNode;

    while (temp != nullptr){
        newNode-> value=temp->value;
        temp=temp->next;
        if (temp !=nullptr){
            newNode->next=new Node;
            newNode=newNode->next;
        }
        else{ newNode->next=nullptr;}
    }
}

LinkedList& LinkedList::operator=(const LinkedList &aCopy){ //assignment operator
    while(root!=nullptr){
        Node* oneBefore= root;
        root =root->next;
        delete oneBefore;
    }
    Node *newNode= new Node;
    Node *temp=aCopy.root;
    root=newNode;

    while(temp!=nullptr){
        newNode->value=temp->value;
        temp=temp->next;
        if(temp!=nullptr){
            newNode->next=new Node;
            newNode=newNode->next;
        }
        else{newNode->next=nullptr;}
    }
    return *this;
}

LinkedList::~LinkedList(){ //dtor
    Node* oneBefore = nullptr;
    while(root!=nullptr){
        oneBefore=root;
        root=root->next;
        delete oneBefore;
    }
}

void LinkedList::append(int value){
    Node* newNode=new Node;
    newNode->value=value;
    if(root!=nullptr){
        Node* temp = root;
        while (temp->next !=nullptr){
            temp=temp->next;
        }
        newNode->next=nullptr;
        temp->next=newNode;
    }
    if(root==nullptr){
        newNode->next=nullptr;
        root=newNode;
    }

}

void LinkedList::prepend(int value){
    Node* newNode=new Node;
    newNode->value=value;
    if (root!=nullptr){
        newNode->next=root;
        root=newNode;
    }
    if(root==nullptr){
        root=newNode;
        newNode->next=nullptr;
    }
}

void LinkedList::remove(int value){
    if(root==nullptr){
        Node *before=nullptr;
        Node *temp=root;
        if(temp->value==value){
            root=temp->next;
        }
        else{
            while(temp->value!=value &&temp->next != nullptr){
                before=temp;
                temp=temp->next;
            }
            if(temp->value==value){
                before->next=temp->next;
            }
        }
        delete temp;
    }
}

int LinkedList::size(){
    Node* aNode = root;
    int numElements=0;
    while(aNode!=nullptr){
        aNode=aNode->next;
        numElements=numElements+1;
    }
    return numElements;
}

LinkedList::Iterator LinkedList::begin(){
    return LinkedList::Iterator(root);
}

LinkedList::Iterator LinkedList::end(){
    Node *aNode=root;
    while(aNode!=nullptr){
        aNode=aNode->next;
    }
    return LinkedList::Iterator(aNode);
}

LinkedList::Iterator Iterator(){
    current=nullptr;
}
LinkedList::Iterator(Node *aNode){
    current=aNode;
}

LinkedList::Iterator operator++(){//I have no idea what the difference is supposed to be between this one and the one below
    current=current->next;
    return *this;
}

LinkedList::Iterator& Iterator::operator=(const LinkedList::Iterator& aCopy){ //assignment operator
    current=aCopy.current;
    return *this;
}

bool Iterator::operator !=(const LinkedList::Iterator& aCopy){
    return current != aCopy.current;
}

bool Iterator::operator==(const LinkedList::Iterator& aCopy){
    return current==aCopy.current;
}

int Iterator::operator*(){
    return current->value;
}

更新:以下建议很有帮助!非常感谢!我遇到的最后一个错误是(我将在下面的编译器中标记出它的位置):  -“隐式声明的副本分配运算符的定义”

更新的hpp文件:


#ifndef linkedlist_h
#define linkedlist_h


  class LinkedList {
  public:


    struct Node {
      //Node(Node *aNext=nullptr) : next(aNext) {}
        int  value;   //this is the value you want to save
        Node *next;  //this points to the next node in the list (or nullptr)
    };
    //friend class Iterator; //do Ineed this even though it's a nested class

    Node *root;

    //---------------------------------------------------------------

    //add a nesTED Interator class...
    class Iterator {
    public:
        Iterator();//default constructor
        //Iterator() : current(nullptr) {}
        Iterator(Node* aNode);
        //Iterator(Node* aNode): current(aNode){};//constructor
        ~Iterator(); //dtor
        Iterator operator++();
        Iterator operator++(int);
        bool operator==(const Iterator &anIterator);
        bool operator!=(const Iterator &anIterator);
        int operator*();
        operator Node*();
        Node *current; //do I need to put LinkedList since it's a nested class?

      //add all the necessary operators

    protected:
    };

    //--------------------------------------------------------------

    LinkedList(); //default constructor...
    LinkedList(const LinkedList& aCopy); //copy ctor
    ~LinkedList(); //dtor
    LinkedList& operator=(const LinkedList& aCopy); //assignment operator

    void append(int value);
    void prepend(int value);
    void remove(int value);
    int size(); //needs to return unsigned int????
    Iterator begin();
    Iterator end();
    Iterator find(int value);


  protected:
  };




#endif /* linkedlist_h */


更新的cpp文件:

#include <stdio.h>
#include "LinkedList.hpp"


//class Iterator;
  LinkedList::LinkedList() {
      root=nullptr;
  }

LinkedList::LinkedList(const LinkedList& aCopy){ //copy ctor
    Node *temp=aCopy.root;
    Node *newNode = new Node;
    root=newNode;

    while (temp != nullptr){
        newNode-> value=temp->value;
        temp=temp->next;
        if (temp !=nullptr){
            newNode->next=new Node;
            newNode=newNode->next;
        }
        else{ newNode->next=nullptr;}
    }
}

LinkedList& LinkedList::operator=(const LinkedList &aCopy){ //assignment operator
    while(root!=nullptr){
        Node* oneBefore= root;
        root =root->next;
        delete oneBefore;
    }
    Node *newNode= new Node;
    Node *temp=aCopy.root;
    root=newNode;

    while(temp!=nullptr){
        newNode->value=temp->value;
        temp=temp->next;
        if(temp!=nullptr){
            newNode->next=new Node;
            newNode=newNode->next;
        }
        else{newNode->next=nullptr;}
    }
    return *this;
}

LinkedList::~LinkedList(){ //dtor
    Node* oneBefore = nullptr;
    while(root!=nullptr){
        oneBefore=root;
        root=root->next;
        delete oneBefore;
    }
}

void LinkedList::append(int value){
    Node* newNode=new Node;
    newNode->value=value;
    if(root!=nullptr){
        Node* temp = root;
        while (temp->next !=nullptr){
            temp=temp->next;
        }
        newNode->next=nullptr;
        temp->next=newNode;
    }
    if(root==nullptr){
        newNode->next=nullptr;
        root=newNode;
    }

}

void LinkedList::prepend(int value){
    Node* newNode=new Node;
    newNode->value=value;
    if (root!=nullptr){
        newNode->next=root;
        root=newNode;
    }
    if(root==nullptr){
        root=newNode;
        newNode->next=nullptr;
    }
}

void LinkedList::remove(int value){
    if(root==nullptr){
        Node *before=nullptr;
        Node *temp=root;
        if(temp->value==value){
            root=temp->next;
        }
        else{
            while(temp->value!=value &&temp->next != nullptr){
                before=temp;
                temp=temp->next;
            }
            if(temp->value==value){
                before->next=temp->next;
            }
        }
        delete temp;
    }
}

int LinkedList::size(){
    Node* aNode = root;
    int numElements=0;
    while(aNode!=nullptr){
        aNode=aNode->next;
        numElements=numElements+1;
    }
    return numElements;
}

LinkedList::Iterator LinkedList::begin(){
    return LinkedList::Iterator(root);
}

LinkedList::Iterator LinkedList::end(){
    Node *aNode=root;
    while(aNode!=nullptr){
        aNode=aNode->next;
    }
    return LinkedList::Iterator(aNode);
}

LinkedList::Iterator::Iterator() : current(nullptr) {}

LinkedList::Iterator::Iterator(Node* aNode): current(aNode){};

LinkedList::Iterator LinkedList::Iterator::operator++(){//I have no idea what the difference is supposed to be between this one and the one below
    current=current->next;
    return *this;
}

LinkedList::Iterator& LinkedList::Iterator::operator=(const LinkedList::Iterator& aCopy) noexcept{ //assignment operator; THIS IS WHERE I SEE AN ERROR
    current=aCopy.current;
    return *this;
}

bool LinkedList::Iterator::operator !=(const LinkedList::Iterator& aCopy){
    return current != aCopy.current;
}

bool LinkedList::Iterator::operator==(const LinkedList::Iterator& aCopy){
    return current==aCopy.current;
}

int LinkedList::Iterator::operator*(){
    return current->value;
}

a376788 回答:如何在hpp文件和cpp文件中正确使用嵌套类?

第一个问题(正如我已经提到的评论)是,嵌套类方法的定义应类似于LinkedList::Iterator::Iterator() { ... }bool LinkedList::Iterator::operator!=(...) { ... }。 (如果有人遇到相同的问题,我再次在这里写下来,以便于查找)

在赋值运算符的定义中出现错误的原因仅仅是因为您没有在标头中声明该类的赋值运算符。

作为奖励回合:operator++()operator++(int)之间的区别在于,第一个用++a(预递增)调用,而另一个用{ {1}}(后递增)。前递增的对象还应返回对同一对象的引用(类似于分配运算符),而后递增的对象应返回对象的副本,与增量之前相同。

本文链接:https://www.f2er.com/3169544.html

大家都在问