Я создал эту программу для назначения. Компилятор выполняет это, но когда я бегу, чтобы проверить выход, он дает следующее сообщение об ошибке:
код приведен ниже:Я создал эту программу, но ее выдача «ошибка сегментации (ядро сбрасывала)», что мне делать?
#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);
}
Я бы бежал к холмам с мокрым фланелем над моим лицом - или использовал отладчик, чтобы сузить его –
_ «Что мне делать» _ Скорее всего, разыгрывание какого-то свисающего или неправильно инициализированного указатель. Используйте отладчик и пройдите через свой код, чтобы узнать подробности. –
@EdHeal У вас все еще есть близкие голоса сегодня? Дайте им один ... –