我创建了这个程序,但它输出“分段错误(核心转储)”,我该怎么办?

时间:2014-11-16 20:33:35

标签: c++ segmentation-fault runtime-error

我创建了这个程序以进行分配。编译器符合它但是当我运行检查输出时,它会出现以下错误:

enter image description here

代码如下:

#include <iostream>
using namespace std;

class Node {
    private:
        int testscore;
        int intmarks;
        char * name;
        Node * nextnode;
    public:
        void settestscore (int testscore) { this -> testscore = testscore; }
        int gettestscore () { return testscore; }
        void setintmarks (int intmarks) { this -> intmarks = intmarks; }
        int getintmarks () { return intmarks; }
        void setname (char * name) { this -> name = name; }
        char * getname () { return name; }
        void setnext (Node * nextnode) { this -> nextnode = nextnode; }
        Node * getnext () { return nextnode; }
};

class List {
    private:
        int size;
        Node * headnode;
        Node * currentnode;
        Node * lastcurrentnode;
    public:
        friend void printlist(List list);
        friend List addcandidates();
        List() {
            headnode = new Node();
            currentnode -> setnext(NULL);
            currentnode = NULL;
            lastcurrentnode = NULL;
            size = 0;
        }
        void addnode(int score, int marks, char * name) {
            Node * newnode = new Node();
            newnode -> settestscore (score);
            newnode -> setintmarks (marks);
            newnode -> setname (name);
            if (currentnode != NULL) {
                newnode -> setnext (currentnode -> getnext());
                currentnode -> setnext (newnode);
                lastcurrentnode = currentnode;
                currentnode = newnode;
            }
            else {
                newnode -> setnext (NULL);
                currentnode -> setnext (newnode);
                lastcurrentnode = headnode;
                currentnode = newnode;
            }
        }
        int gettestscore () {
            if (currentnode != NULL)
                return currentnode->gettestscore();
        }
        int getintmarks () {
            if (currentnode != NULL)
                return currentnode->getintmarks();
        }
        char * getname () {
            if (currentnode != NULL)
                return currentnode->getname();
        }
        bool next() {
            if (currentnode == NULL) {
                return false;
            }
            lastcurrentnode = currentnode;
            currentnode = currentnode -> getnext();
            if (currentnode == NULL || size == 0) {
                return false;
            }
            else
                return true;
        }

};

List addcandidates() {
    List list;
    list.addnode(50, 20, "Annie Khalid");
    list.addnode(35, 30, "Humaira Arshad");
    list.addnode(37, 29, "Atif Aslam");
    list.addnode(59, 10, "Qurat-ul-Ain Baloch");
    list.addnode(25, 9, "Sanam Marvi");
    list.addnode(44, 11, "Ali Zafar");
    list.addnode(59, 16, "Farhan Saeed");
    list.addnode(50, 22, "Amanat Ali");
    list.addnode(60, 28, "Junaid Jamshed");
    list.addnode(78, 17, "Shahzad Roy");
    list.addnode(78, 15, "Ali Azmat");
    list.addnode(40, 30, "Nadeem Abbas");
    return list;
}

void printlist(List list) {
    Node * tempnode = list.currentnode;
    list.currentnode = list.headnode;
    for (int i = 1; list.next(); i++) {
    cout << i << ": " << list.getname() << "\t \t" << list.gettestscore() << "\t \t" << list.getintmarks() ;
    }
    list.currentnode = tempnode;
}

main() {
    List list = addcandidates();
    printlist(list);
}
我找不到问题所在?我需要一些帮助。 提前致谢。

1 个答案:

答案 0 :(得分:1)

错误在于List的构造函数:

List() {
    headnode = new Node();
    currentnode->setnext(NULL); // <--- error is here!
    currentnode = NULL;
    lastcurrentnode = NULL;
    size = 0;
}

此时你的指针currentnode为NULL(之后你也将它设置为NULL,这没有任何意义)。您首先必须创建一个新的Node并将其分配给currentnode才能使其正常工作,就像您使用headnode一样,或者将headnode分配给currentnode直接(因为看起来你使用currentnode进行迭代,这会在构造过程中“正确”设置)。

取消引用(调用setnext()currentNode时,如果它为NULL,则程序崩溃。

你的代码中还有很多其他的东西,你应该彻底修改它并进入指针语义的一些教程。但只是回答你的问题,标记的行是你的问题。

此外,使用调试器运行此代码并通过代码进行操作非常简单,并且可以轻松找到这类错误:)

编辑,就像单挑:即使你解决了这个问题,你也不会打印清单。您对currentnodeheadnode的管理不正确,因此您会收到错误的链接列表,并且在更正后,您的打印也会出错。抱歉打破这个消息...