繁体   English   中英

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

[英]How do I properly use nested classes in my hpp file and cpp file?

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

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

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

如果我弄清楚我的代码有什么问题,我将继续做更多的研究并在这里报告,希望能在其他人有同样的问题时提供帮助。

提前非常感谢!

这是我的 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;
}

第一个问题(正如我在评论中已经说过的)是,嵌套 class 方法的定义应该看起来像LinkedList::Iterator::Iterator() {... }bool LinkedList::Iterator::operator.=(...) {... } (我只是在这里再写一次,以便更容易找到,如果其他人有同样的问题)

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

作为奖励回合: operator++()operator++(int)之间的区别在于,第一个使用++a (前增量)调用,而另一个使用a++ (后增量)调用。 前增量还应该返回对相同 object 的引用(类似于 assingment 运算符),而后增量返回 object 的副本,就像它在增量之前一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM