【问题标题】:QT Write access violationQT 写访问冲突
【发布时间】:2016-12-26 21:25:26
【问题描述】:

大家好,我正在尝试通过 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);
}

【问题讨论】:

  • newrec->next 的 thw 类型是什么?你可以分享项目类的代码吗?
  • @t.m.是的,当然,完全忘记了:)
  • 我不确定在c++中是不是这样,但是在c结构中,如果你本身使用结构的关键字,你应该将它显式地写为结构。我的意思是,写“struct item *next”而不是“item * next”。
  • 如何创建first 项目? proc_list.cpp 第 11 行究竟发生了什么?哦。等待。 this 在那时如何合法?另外,为什么不使用std::vectorQVectorQList
  • item 结构没有这样的构造函数时,你为什么要调用newrec = new item(this);

标签: c++ qt linked-list qstring


【解决方案1】:

我注意到的第一件事是您提到了过程编程,然后在函数中使用了“this”指针。只需将其删除。您甚至没有要使用的“item”结构的构造函数。

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

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

编辑:

好吧,我设法用纯 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);
}

【讨论】:

  • 我必须承认我没有看到这个问题,但修复了它,它仍然给我错误。当我想将名称分配给 newrec->name 时,它出现在第 11 行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2021-02-16
  • 2017-08-23
  • 2016-05-17
  • 1970-01-01
相关资源
最近更新 更多