2017-02-28 53 views
0

即时编程使用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 
     } 
} 
+0

TLDR:我用/宣告我的目标很可能是错的,你应该专注于从ListeContact对象“清单当然”和fonction GET(指数)的方式(通过链接列表“指数”量并返回一个联系人,另外,我不能创建与LLContactAdd的不同联系人,它需要我输入的最后一个联系人并创建多个副本,这可能是该bug的一部分。 –

回答

1

的主要问题是由于增加了一个小角色'&'class Node !!!

问题 - 使用局部变量Contact contact;并通过值发送它,然后通过参考高达class Node

MainWindow::on_pbAjouter_clicked(),当你被liste.lLContactAdd(contact);添加 接触,它是由值(void lLContactAdd(Contact c);)。

然后从ListeContactNode它是通过使用上堆栈数据参考 Node(Contact &c)发送要被存储在NodeContact &_c;)。

即使在main函数中创建新的Contact,结果也会从堆栈值中存储。

解决方案 - 只是迫使Node通过移除&创建Contact

ListeContact创建的本地Contact c将存储在 的NodeContact _c;

class Node 
{ 
public: 
    // Contact &_c; // to be replaced 
    Contact _c; 
    Node(Contact &c); 
    Node * _next; 
}; 
+0

哇,我的救星!!!! IT WORKS。谢谢。许多! –