2017-02-28 2 views
0

qtとiveを使ってimプログラミングが解決方法を知らないセグメンテーションフォルトを見つけました。プログラムの目的は、ボタンを押したときにインターフェースを介してリンク先リストに連絡先を追加することです。連絡先を姓または名でソートすることもできます。私はかなり問題がクラスlinkedlist(私はmainwindow.h、公共でそれを宣言)の私のオブジェクト "リスト"から来ていることを確認してください。 「リスト」はすべての連絡先のリストです。 MainWindow :: on_pbAjouter_clicked()で行われているlLContactAdd経由でリストに連絡先を追加できます。しかし、オブジェクト "list"は、void MainWindow :: on_rbPreN_clicked()のような他の機能のために存在しないようです。私は関数on_rbPreN_clicked()に移動し、 "get()"を使用してリストから連絡先を取得すると、デバッグ時にセグメント化エラーが発生しますが、LLContactAdd()では1つも与えません。ノード内に戻るか、リスト全体が存在しません。 また、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 "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(インデックス)からオブジェクト「LISTE」に焦点を当てる必要があります(リンクリストを「インデックス」金額を渡しますまた、私はLLContactAddとの別の連絡先を作成することはできません。最後に入力した連絡先がバグの一部である複数のコピーを作成します。 –

答えて

1

主な問題は、で追加され、小さな文字'&'によるものであるメインを検討するものです。//

#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) 
{ 

} 

class Node !!!

問題 - ローカル変数Contact contact;を使用し、それを値で送信し、class Nodeまで参照してください。 MainWindow::on_pbAjouter_clicked()

、あなたがliste.lLContactAdd(contact);で 連絡先を追加しているとき、それは値(void lLContactAdd(Contact c);)です。

そしてListeContactからNodeには、スタック上のデータを用いて基準 Node(Contact &c)によって送信されNodeContact &_c;)に格納されます。

メイン関数に新しい Contactを作成した場合でも、結果はスタック値から格納されます。

ソリューション - ちょうど&を除去することにより、Contactを作成するNodeを強制します。 ListeContactで作成

ローカルContact cNodeの新しいContact _c;に保存されます。

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

うわー、私の救い主!!!! IT WORKS。ありがとう多く! –

関連する問題