即時編程使用qt和ive遇到了一個我不知道如何解決的分段錯誤。該程序的目的是通過界面添加一個聯繫人到一個鏈接列表,當我按下一個按鈕時,它也可以按他們的名字或姓氏對聯繫人進行排序。林非常確定問題來自我的對象「列表」在類鏈表(我聲明它在mainwindow.h,公共)。 「列表」是所有聯繫人的列表。我可以通過在void MainWindow :: on_pbAjouter_clicked()中完成的lLContactAdd在列表中添加一個聯繫人。然而,對象「列表」似乎不存在像void MainWindow :: on_rbPreN_clicked()的其他功能。當我進入函數on_rbPreN_clicked()並使用「get()」從列表中檢索聯繫人時,它在調試時給了我一個分段錯誤,但它並沒有給我一個在LLContactAdd()中,可能是因爲這些在Node中返回或者整個程序不存在的列表。 另外,LLContactAdd()會一直添加相同的聯繫人(我輸入的最後一個聯繫人)。 這裏的程序,很抱歉,如果它相當大。如果你不瞭解它,請隨時向我詢問部分代碼。我在mainwindow.cpp中留下了一些註釋。我希望你們能幫助我! *對於平庸的英語感到抱歉,我是法國學生。 請讓我知道,如果你不明白我的解釋的一部分病態準備好給更多的信息。嘗試從鏈接列表返回數據時qt中的分段錯誤
#ifndef CONTACT_H
#define CONTACT_H
#include <QString>
#include <iostream>
class Contact
{
QString _prenom;
QString _nom;
QString _tel;
QString _email;
public:
Contact();
Contact(QString prenom,QString nom, QString tel, QString email);
QString prenom();
void setPrenom(QString prenom);
QString nom();
void setNom(QString nom);
QString tel();
void setTel(QString tel);
QString email();
void setEmail(QString email);
};
#endif // CONTACT_H
#ifndef LISTECONTACT_H
#define LISTECONTACT_H
#include <contact.h>
#include "node.h"
#include <QString>
class ListeContact
{
Node *head = 0;
int _size;
Contact _c;
public:
ListeContact();
void lLContactAdd(Contact c);
// ~ListeContact();
Contact & get(int index);
void remove(int index);
int getSize();
};
#endif // LISTECONTACT_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "listecontact.h"
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
ListeContact liste; // this is where i create a global object which is my list(im not sure this works)
private slots:
void on_pbAjouter_clicked();
void on_rbNomP_clicked();
void on_rbPreN_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef NODE_H
#define NODE_H
#include "contact.h"
class Node
{
public:
Contact &_c;
Node(Contact &c);
Node * _next;
};
#endif // NODE_H
現在CPP
#include "Contact.h"
#include<QString>
#include "listecontact.h"
Contact::Contact()
{
}
Contact::Contact(QString prenom, QString nom, QString tel, QString email) :
_prenom(prenom),_nom(nom),_tel(tel), _email(email)
{}
QString Contact::prenom(){
return _prenom;
}
void Contact::setPrenom(QString prenom){
_prenom= prenom;
}
QString Contact::nom(){
return _nom;
}
void Contact::setNom(QString nom){
_nom= nom;
}
QString Contact::tel(){
return _tel;
}
void Contact::setTel(QString tel){
_tel=tel;
}
QString Contact::email(){
return _email;
}
void Contact::setEmail(QString email){
_email = email;
}
#include "listecontact.h"
#include <QDebug>
#include "contact.h"
ListeContact::ListeContact():head(0),_size(0)
{
}
void ListeContact::lLContactAdd(Contact c)
{
Node * tmp = new Node(c);
if(head)
{
Node * current = head;
while(current->_next) current = current->_next;
current->_next=tmp;
}
else head = tmp;
_size++;
qDebug()<<_size;
}
int ListeContact::getSize()
{
return _size;
}
/*ListeContact::~ListeContact()
{
Node * current = head;
while(current)
{
Node* tmp = current;
current = current->_next;
delete tmp;
}
}*/
Contact & ListeContact::get(int index)
{
Node * current = head;
while(index>0 && current->_next)
{
current = current->_next;
index--;
}
return current->_c; // this is where the debug put the segmentation fault but only when i call the funtion void MainWindow::on_rbPreN_clicked() or void MainWindow::on_rbNomP_clicked()
}
#include "mainwindow.h"
#include <QApplication>
#include "listecontact.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "node.h"
Node::Node(Contact &c): _c(c),_next(0)
{
}
//這是什麼你們會考慮在main()
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "listecontact.h"
#include "contact.h"
#include <QString>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pbAjouter_clicked()//the program almost work in this function apart from the fact that it always takes the same contact over and over again
{
int type;
if(ui->rbPreN->isChecked()) type=0;
else if(ui->rbNomP->isChecked()) type=1;
Contact contact;
contact.setPrenom(ui->lnPrenom->text().replace(";","_"));//ignore this part
contact.setNom(ui->lnNom->text().replace(";","_"));
contact.setTel(ui->lnTel->text().replace(";","_"));
contact.setEmail(ui->lnEmail->text().replace(";","_"));
liste.lLContactAdd(contact);//Here, i add the contact to my linked list
ui->listWidget->clear();
if(type){
for(int i = 0; i < liste.getSize();i++) //I display the contact on my interface
{
ui->listWidget->addItem(liste.get(i).nom()+liste.get(i).prenom());
//ui->listWidget->addItem(contact.nom())+(",")+(contact.prenom()); // i can add as many contact as i want but it will always be the same one (the last one i added)
}
}
else{
for(int i = 0; i < liste.getSize();i++)
{
ui->listWidget->addItem(liste.get(i).prenom()+liste.get(i).nom());
}
}
}
void MainWindow::on_rbPreN_clicked() // the program crashes here this is where i sort the list
{
int a = liste.getSize();
ui->listWidget->clear();
//tribulle(1);
for(int i = 0; i < liste.getSize();i++)
{
ui->listWidget->addItem(liste.get(i).prenom()+(",")+liste.get(i).nom()); // it gives me a segmentation fault when i try to return my current pointer
}
}
TLDR:我用/宣告我的目標很可能是錯的,你應該專注於從ListeContact對象「清單當然」和fonction GET(指數)的方式(通過鏈接列表「指數」量並返回一個聯繫人,另外,我不能創建與LLContactAdd的不同聯繫人,它需要我輸入的最後一個聯繫人並創建多個副本,這可能是該bug的一部分。 –