【问题标题】:Segmentation fault; core dumped in my code c++分段故障;核心转储在我的代码 C++ 中
【发布时间】:2017-09-11 21:03:13
【问题描述】:

每次我运行它都会给我这个错误分段错误;核心转储; 我试图在 C++ 中做一个linkedStack。 我的代码是:

节点.h

class Node {
public:
    Node(int element);
    const int& getElement()const;
    Node *getNext() const;
    void setNext(Node *e);
    Node(const Node& orig);
    virtual ~Node();

private:
    int element;
    Node *next;
};

节点.cpp

#include "Node.h"
Node::Node(int element) {
    this->element=element;
}
const int& Node::getElement() const{
    return element;
}
Node * Node::getNext() const{
    return next;
}
void Node::setNext(Node *e){
    next=e;
}

Node::Node(const Node& orig) {
}

Node::~Node() {
}

LinkedStack.h

#include "Node.h"
#include <iostream>
#include "EmptyException.h"


class LinkedStack {
public:
    LinkedStack();
    int size() const;
    const Node& top() const;
    void push(const int& element);
    void pop();
    void print();
    LinkedStack(const LinkedStack& orig);
    virtual ~LinkedStack();
private:
    Node *front=NULL;
    int num_elements;
};

LinkedStack.cpp

#include "LinkedStack.h"
using namespace std;

LinkedStack::LinkedStack() {
}
int LinkedStack::size() const{
    return num_elements;
}
const Node& LinkedStack::top() const{
    return *front;
}
void LinkedStack::push(const int& element){
    Node *newfront=new Node(element);
    newfront->setNext(front);
    front=newfront;
    delete newfront;
    num_elements++;
}
void LinkedStack::pop(){
    if(num_elements==0){
        throw EmptyException();
    }
    else{
        Node *oldfront=front;
        front=front->getNext();
        num_elements--;
    }
}
void LinkedStack::print(){
    Node *temp=front;
    while(temp != __null){
        cout<<temp->getElement()<<endl;
        temp=temp->getNext();
    }
    cout<<""<<endl;
}

LinkedStack::LinkedStack(const LinkedStack& orig) {
}

LinkedStack::~LinkedStack() {
}

main.cpp

#include <cstdlib>
#include <iostream>
#include <string>
#include "LinkedStack.h"

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    string menu[]={"1.Afegir","2.Eliminar","3.Mostrar","4.Sortir"};
    int opc,element;
    LinkedStack Stack;
    do{
        for(int i=0;i<4;i++){
            cout<<menu[i]<<endl;
        }
        cout<<"Selecciona una opció"; cin>>opc; cout<<""<<endl;
        switch(opc){
            case 1:
                cout<<"Que vols afegir?... "; cin>>element; cout<<""<<endl;
                Stack.push(element);
                break;
            case 2:
                cout<<"Eliminant.... "<<endl;
                Stack.pop();
                break;
            case 3:
                Stack.print();
                break;
        }

    }while(opc!=4);

    return 0;
}

就是这样。 当我尝试第一个选项(推送)时没有问题,但是当我尝试弹出或打印堆栈时,它给了我核心转储:分段错误错误。

我认为问题在于指针(??),但我仍然不知道在哪里或如何。

如果你能帮上忙就好了^^

【问题讨论】:

标签: c++ pointers memory segmentation-fault stack


【解决方案1】:

push 方法存在问题,导致pop 中的分段方法。在以下几行中

newfront->setNext(front);
front=newfront;
delete newfront;

您将下一个节点设置为front 指向的对象,该对象稍后将被删除,因为front=newfront 设置了指向同一对象的指针。

分段错误出现在行

front=front->getNext();

一个空对象被取消引用。

【讨论】:

  • 我知道错误在哪里,但仍然不知道如何解决:/ 我尝试了一些更改,但仍然无法正常工作
【解决方案2】:

遵循 Neil Butterworth 的思路。 __null 是什么?是你定义的宏吗?

使用nullptr

void LinkedStack::print(){
    Node *temp = front;
    while(temp != nullptr){ // compare it against nullptr if this is Modern C++ (e.g. C++11). For previous C++ standards you can use temp != 0 or !temp
        cout << temp->getElement()<<endl;
        temp = temp->getNext();
    }
    cout << endl;
}

此外,正如 aschepler 所述,您的节点构造函数需要设置为空值。

Node::Node(int element) {
    this->element = element;
    this->next = nullptr;
}

【讨论】:

  • 我使用您的建议更改了我的代码,但仍然无法像@ImrePiller 所说的那样工作,问题在于在 pop in front=front->getNext(): bcs of the front=newfront in推。但不知道如何解决。
猜你喜欢
  • 2016-09-09
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多