【发布时间】:2017-02-28 19:38:49
【问题描述】:
我用 qt 和 ive 编程遇到了一个我不知道如何解决的分段错误。该程序的目的是在我按下按钮时通过界面将联系人添加到链接列表中,它还可以按联系人的名字或姓氏对联系人进行排序。我很确定问题来自类链表中的对象“List”(我在 mainwindow.h 中声明它,public)。 “列表”是所有联系人的列表。我可以通过在 void MainWindow::on_pbAjouter_clicked() 中完成的 lLContactAdd 在列表中添加联系人。但是,对于像 void MainWindow::on_rbPreN_clicked() 这样的其他函数,对象“列表”似乎不存在。当我进入函数 on_rbPreN_clicked() 并使用“get()”从列表中检索联系人时,它在调试中给了我一个分段错误,但在 LLContactAdd() 中没有给我一个分段错误,可能是因为什么都没有在节点中返回或整个程序的列表不存在。 此外,LLContactAdd() 将始终一遍又一遍地添加相同的联系人(我输入的最后一个联系人)。 这是程序,对不起,如果它很大。如果您不理解,请随时向我询问部分代码。我在 mainwindow.cpp 中留下了一些 cmets。我希望你们能帮助我! *对不起平庸的英语,我是法国学生。 如果您不理解我的解释的某些部分,请告诉我,我准备好提供更多信息。
#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 中的对象“liste”和函数 get(index)(通过链表“index”数量并返回一个联系人。此外,我无法使用 LLContactAdd 创建不同的联系人,它会获取我输入的最后一个联系人并制作多个副本,这可能是错误的一部分。
标签: c++ qt linked-list segmentation-fault