简体   繁体   English

使用类的C ++链接列表(OOP)

[英]C++ Linked list using Classes (OOP)

This is my first post on StackOverflow, as I am genuinely stuck. 这是我在StackOverflow上发表的第一篇文章,因为我确实被卡住了。 My issue is that everytime I run the following code, at the first call for the function InsertNode(), the return temp node has the correct values for next node and data. 我的问题是,每次运行以下代码时,在第一次调用函数InsertNode()时,返回临时节点的下一个节点和数据的值均正确。 However, when the function is called again, for some reasons the head gets reset to data NULL and next pointer recursively pointing to the same address. 但是,当再次调用该函数时,由于某些原因,头将重置为数据NULL,并且下一个指针递归指向同一地址。 I'm having a hard time implementing this in OOP, I have done this using plain structs successfully. 我很难在OOP中实现它,我已经成功地使用了简单的结构来实现了。 But with OOP I'm confused as how to declare the Node* Node::InsertNode(Node* head), method in main, as I get an error that InsertNode is undeclared. 但是对于OOP,我对如何声明main *中的Node * Node :: InsertNode(Node * head)这个方法感到困惑,因为我收到一个未声明InsertNode的错误。 So as workaround I declared InsertNode outside of the Node class as an independent function. 因此,作为解决方法,我在Node类之外将InsertNode声明为独立函数。 I have a feeling that is what may be causing the issue. 我感觉可能是导致此问题的原因。 would appreciate some help on what is going on or what I should change in my code. 希望对正在发生的事情或应该在代码中进行的更改提供一些帮助。 Thank you! 谢谢!

hashtable.cpp hashtable.cpp

#include "Hashtable.hpp"
using namespace std; 
Node::Node(){

    data = NULL;
   Node* nextP = NULL;
};

Node::~Node(){

}

Node* InsertNode(Node* head, int data){

    Node* temp = new Node();

    if(head->nextP == NULL){

        head->data = data;
        temp->nextP = head;
        head = temp;

    } else if(head->nextP!=NULL){

        temp->nextP = head;
        temp->data = data;
        head = temp;
    }

    return head;
};

void Node::printNode(Node* head){
    Node* temp = new Node();
    temp = head;
    while(temp->nextP != NULL){
        printf("%d\n", temp->data);
        temp = temp->nextP;
    }
}

Hashtable.hpp Hashtable.hpp

#ifndef Hashtable_hpp
#define Hashtable_hpp

#include <stdio.h>

class Node
{
public:
    Node* nextP;
    Node();
    ~Node();

    void printNode(Node* head);
    int data = NULL;

    private:


};
Node* InsertNode(Node* head, int data);

#endif /* Hashtable_hpp */

main.cpp main.cpp中

#include <iostream>
#include "stdio.h"
#include <string>
#include "Hashtable.hpp"

using namespace std;
Node head;
//Node* head = new Node();


int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
   head =  *InsertNode (&head, 10);
   // head = temp2;
   head =  *InsertNode (&head, 20);
   // head = temp2;
   head =  *InsertNode (&head, 30);
   // head = temp2;

     //InsertNode(head, 20);

  Node printNode(head);

    return 0;


}

So I finally figured out the issue. 所以我终于弄清楚了这个问题。 Since initially I was referencing the class function InsertNode() directly, I was trying avoid the error that I was other getting with the undeclared identifier. 因为最初我是直接引用类函数InsertNode()的,所以我试图避免使用未声明的标识符产生的其他错误。 So as a work around I moved the function outside of the class declaration, which then caused more problems as you saw above post. 因此,作为一项变通办法,我将函数移到了类声明之外,这又引起了更多问题,如您在上面的文章中所看到的。 Now I realized that when the function exists inside the Class, I have reference it by first de-referencing (my terminology is probably wrong) the function using the following: head->InsertNode(head, data); 现在我意识到,当函数存在于类中时,我已经通过以下方式首先取消引用(我的术语可能是错误的)来引用该函数:head-> InsertNode(head,data); I was initially trying different iterations of InsertNode(&head, data) or Node* InsertNode(&head, data) ... etc. Basically trying to brute force my way through the compiler :). 最初,我尝试使用不同的InsertNode(&head,data)或Node * InsertNode(&head,data)...等迭代。基本上,我尝试通过编译器以蛮力方式:)。

I am attaching the code below, please let me know your comments on what I can improve. 我附上以下代码,请让我知道您对我可以改进的意见。

Hashtable.cpp Hashtable.cpp

#include "Hashtable.hpp"
#include <iostream>

using namespace std;

Node::Node(){

    data = NULL;
    Node* nextP = NULL;
};

Node::~Node(){

}

Node* Node::InsertNode(Node* head, int data){
    Node* temp = new Node();
    if(head->nextP == NULL){
        head->data = data;
        temp->nextP = head;

    } else if(head->nextP!=NULL){
        temp->nextP = head;
        temp->data = data;
    }

    return temp;
};


void Node::printNode(Node* head){
    Node* temp = new Node();
    temp = head;
    while(temp->nextP != NULL){
        printf("%d\n", temp->data);
        temp = temp->nextP;
    }
}

Hashtable.hpp Hashtable.hpp

#ifndef Hashtable_hpp
#define Hashtable_hpp

#include <stdio.h>
#include <iostream>

using namespace std;

class Node
{
    int data = NULL;
    Node* nextP;
public:

    Node();
    ~Node();
    Node* InsertNode(Node* head, int data);
    void printNode(Node* head);
        private:
};


#endif /* Hashtable_hpp */

main.cpp main.cpp中

#include <iostream>
#include "stdio.h"
#include <string>
#include "Hashtable.hpp"

using namespace std;
Node* head = new Node();

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    Node temp2;
    head =  head->InsertNode (head, 10);
    head =  head->InsertNode (head, 20);
    head =  head->InsertNode (head, 30);
    head =  head->InsertNode (head, 40);
    head =  head->InsertNode (head, 50);
    head =  head->InsertNode (head, 60);
    head =  head->InsertNode (head, 70);
     head->printNode(head);

    return 0;

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

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