QT写入访问冲突

时间:2016-12-26 21:25:27

标签: c++ qt linked-list qstring

嘿伙计们,我正在尝试通过QT中的cpp程序编程来制作链表,每当我尝试在列表中添加内容时,我都会收到此错误:

c:\users\marcin\documents\dev cpp\proc_list\proc_list.cpp:11: error: Exception at 0x13fc325cb, code: 0xc0000005: write access violation at: 0x1, flags=0x0 (first chance)

从我已经读过的问题应该是我尝试访问空指针,但尝试检查它,它看起来很好。这是错误的代码:

void append_rec(QString name, int age, int number, float balance, item *first){
    item *newrec;
    item *temp;

    newrec = new item(this);
    temp = new item;

    newrec->next = NULL;
    newrec->name = name;
    newrec->number = number;
    newrec->balance = balance;
    newrec->age = age;
    temp = first;

    while(temp->next!= NULL)
        temp = temp->next;

    temp->next = newrec;
}

和问题(正如调试器在newrec->next = NULL;行上弹出的那样。我刚开始学习cpp而且认真找不到解决方案。

修改

项结构的代码(对于此赋值,我不允许使用类):

#ifndef PROC_LIST_H
#define PROC_LIST_H

#include <qstring.h>

struct item{
    item *next;
    QString name;
    int age;
    int number;
    float balance;
};

void append_rec(QString name, int age, int number, float balance, item * first);
void display_list( item * first );

#endif // PROC_LIST_H

编辑2

主窗口文件,涵盖了我对列表所做的所有事情。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "proc_list.cpp"

item *first = NULL;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_front_add_clicked()
{
    append_rec(ui->name_edit->text(),
               ui->age_edit->text().toInt(),
               ui->accnum_edit->text().toInt(),
               ui->balance_edit->text().toFloat(),
               first);
}

1 个答案:

答案 0 :(得分:-1)

我注意到的第一件事是你提到了程序编程,然后使用了&#34;这个&#34;函数中的指针。只需删除它。你甚至没有#34;项目的构造函数&#34;要使用的结构。

其次,您执行temp = new item项目,然后立即执行此temp = first。 BAM内存泄漏。但这不是一个主要问题。

另请注意您的列表基指针从一开始就是NULL并尝试从您的while中取消引用其成员temp->next可能会返回垃圾结果并且MAYBE评估为true,即使它只是垃圾。 例如,STL列表具有&#34; end&#34;指针避免那种事情,你也可以这样做。 希望我帮忙!

修改

我设法解决了普通C ++中的问题,但我可能做得过于复杂。 我选择使用指针指针,因为当列表为空时,我们将能够通过参数更改基指针。但是,我不认为这是最佳解决方案,但它确实有效。请注意,您现在需要在从main调用时传递项目的地址。

#include <iostream>

using namespace std;

struct item{
    item *next;
    string name;
    int age;
    int number;
    float balance;
};

void append_rec(string name, int age, int number, float balance, item** first){
    item *newrec = new item;

    newrec->next = NULL;
    newrec->name = name;
    newrec->number = number;
    newrec->balance = balance;
    newrec->age = age;

    while(*first!= NULL) // The pointer we are pointing to isnt valid
        first = &(*first)->next; // Point to the next pointer in the list

    *first = newrec; // Change the value of the pointer we are pointing to
}
void display_list( item * first )
{
    item* temp = first;
    while ( temp != nullptr )
    {
        cout << temp->name << '\n';
        cout << temp->number << '\n';
        cout << temp->balance << '\n';
        cout << temp->age << '\n';
        cout << "<===================||=======================>";

        temp = temp->next;
    }
}

item *first = NULL;
int main()
{
    append_rec("something 1", 1, 1, 1.f, &first);
    append_rec("something 2", 2, 2, 2.f, &first);
    append_rec("something 3", 3, 3, 3.f, &first);
    append_rec("something 4", 4, 4, 4.f, &first);

    display_list(first);
}