简体   繁体   English

C ++分段错误:将字符串传递给链表中的节点

[英]C++ Segmentation Fault: Passing a string to a node in a linked list

I'm new to working with class templates and am simply trying to define a temporary node 'temp' in a class associated with the Linked List, which sets the string that the node stores to some temporary string that is created in the function TowerHanoi::set_Discs(size_t disc) via user input. 我是使用类模板的新手,只是想在与链接列表相关的类中定义一个临时节点“ temp”,该节点将节点存储的字符串设置为在TowerHanoi::set_Discs(size_t disc)函数中创建的某些临时字符串TowerHanoi::set_Discs(size_t disc)通过用户输入。 When I call the function temp->set_data(tmp_str) i get a segmentation fault. 当我调用函数temp->set_data(tmp_str)出现分段错误。 I tried calling temp->set_data("hello"); 我尝试调用temp->set_data("hello"); on its own and i still get the error. 单靠它,我仍然会收到错误消息。 I'm not sure what's going on here and i've tried researching into it but to no avail. 我不知道这是怎么回事,我已经尝试研究它,但无济于事。 I'm probably missing something obvious, but i'm just quite lost now. 我可能缺少明显的东西,但是现在我很失落。 Let me know if you need more code. 让我知道您是否需要更多代码。 Thanks. 谢谢。

TowerHanoi.cpp: TowerHanoi.cpp:

#include "TowerHanoi.h"
#include <iostream>
#include <cstdlib>

using namespace std;
using oreilly_A2::node;

namespace oreilly_A2 {
    TowerHanoi::TowerHanoi() {
        for (int i=0;i<2;i++) {
            rod[i] = LStack<node<std::string> >();
        }
    }

    TowerHanoi::TowerHanoi(size_t numDiscs) {
        for (int i=0; i < 2; i++) {
            rod[i] = LStack<node<string> >();
        }
        discs = numDiscs;

    }

    void TowerHanoi::set_Discs(size_t disc) {
        node<string>* temp=NULL;

        while (disc != 0) {
            string tmp_str;
            for (size_t i=0; i<disc; i++) {
                tmp_str.append("x");

            }
            disc--;
        temp->set_data(tmp_str);
        rod[0].push(temp);
    }

    void TowerHanoi::print_Game() {

        for (size_t s=1; s<discs; s++) {
                cout << "   "; 
                    for (size_t o=1; o<discs-s;o++) {
                        cout << " ";
                    }
                //cout << tmp_node->data() << endl;
                    cout << "x" << endl;
            }
    }

}

node.h file: node.h文件:

#ifndef NODE_CAMERON_H
#define NODE_CAMERON_H
#include <string>

namespace oreilly_A2 {
    template <typename Item>
        class node {

        public:

        node(); //constructor for node

        node(const Item val, node* newNext); //constructor with parameters

        ~node(); //destructor

        void set_data(Item new_data); //set the word that this node contains

        void set_link(node* new_link); //set the 'next' node
        void set_previous(node* new_prev);

        Item data() const; //return this node's word

        const node* link() const; //return next

        const node* back() const;

        node* link(); //return next

        node* back(); 

        private:

        node* next; //the next node
        node* previous;
        Item word; //the word this node contains

        };
}
#include "Node.template"
#endif

node.template file: node.template文件:

    namespace oreilly_A2 {


    template <typename Item>
    node<Item>::node() {
        next=NULL;
        previous=NULL;
    }
    //Node.template
    template <typename Item>
    node<Item>::node(const Item val, node* newNext=NULL) {
        word = val;
        next = newNext;
    }

    template <typename Item>
    node<Item>::~node() {
        delete next;
        delete previous;
        delete word;
    }

    template <typename Item>
    void node<Item>::set_data(Item new_data){
            word = new_data;
    }


    template <typename Item>
    void node<Item>::set_link(node* new_link){
            next = new_link;

    }


    template <typename Item>
    void node<Item>::set_previous(node* new_back) {
            previous = new_back;
    }


    template <typename Item>
    Item node<Item>::data() const {  //return the word
            return word;
    }


    template <typename Item>
    const node<Item>* node<Item>::link() const { //return next node (const function)
            return next;
    }


    template <typename Item>
    const node<Item>* node<Item>::back() const { //return previous node (const)
            return previous;
    }


    template <typename Item>
    node<Item>* node<Item>::link() {
            return next; //return next node (non-const)
    }


    template <typename Item>
    node<Item>* node<Item>::back() { //return previous node (const)
            return previous;
    }

}

Unless I have missed something the temp variable is NULL at the time of calling set_data . 除非我错过了一些事情,否则在调用set_datatemp变量为NULL As any regular object you need to first initialized it. 作为任何常规对象,您需要首先对其进行初始化。

node<string>* temp=new node<string>();

And then freeing it when appropriate to avoid memory leaks. 然后在适当的时候释放它以避免内存泄漏。

This is not the case with temp_str because the later is not a pointer, it's a value so it gets initialized automatically (and also freed automatically when it gets out of scope). temp_str不是这种情况,因为后者不是指针,而是一个值,因此它会自动初始化(并且在超出范围时也会自动释放)。

You have initialized temp as NULL . 您已将temp as NULL初始化temp as NULL So when you are trying to do temp->set_data(tmp_str); 因此,当您尝试执行temp->set_data(tmp_str); you are actually trying to access NULL pointers. 您实际上正在尝试访问NULL指针。

All you need to do is initialize temp . 您需要做的只是初始化temp I have correct the code below 我有下面的正确代码

 void TowerHanoi::set_Discs(size_t disc) {
            node<string>* temp=new node<string>();

            while (disc != 0) {
                string tmp_str;
                for (size_t i=0; i<disc; i++) {
                    tmp_str.append("x");

                }
                disc--;
            temp->set_data(tmp_str);
            rod[0].push(temp);
        }

To avoid memory leak you need to delete all the memory allocated after you are done. 为避免内存泄漏,您需要在完成后删除所有分配的内存。

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

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