简体   繁体   English

当我尝试在地图中插入一对Qt程序段错误

[英]Qt program seg faults when I try to insert a pair into a map

I am working on my first Qt application, widget really, and I am getting a segfault when I try to fill up a standard library map with a <int, QString> pair. 我正在开发我的第一个Qt应用程序,即窗口小部件,当尝试使用<int, QString>对填充标准库映射时,我遇到了段错误。 My goal is to fill the map with int keys and QString values. 我的目标是用int键和QString值填充地图。 I don't know if pair is the best way to do this, so any advice would be great. 我不知道是否pair是执行此操作的最佳方法,因此任何建议都很好。

Here is the only source file besides the main. 这是除主文件外唯一的源文件。

#include "linuxtips.h"
#include "ui_linuxtips.h"

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

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

void LinuxTips::on_learnMore_clicked()
{

}

void LinuxTips::on_viewAll_clicked()
{

}

void LinuxTips::loadRandTip()
{
  int i = 0;
  std::map<int, QString>::iterator it;

  QString line;
  QFile inputFile(":/tipFile.txt");
  inputFile.open(QIODevice::ReadOnly);

  QTextStream in(&inputFile);
  do{
    line = in.readLine();
  //  this->TipMap.insert(it, std::pair<int, QString >(i,line));
    i++;
  }while(!in.atEnd());
}

If I uncomment this->TipMap.insert(it, std::pair<int, QString >(i,line)); 如果我取消注释this->TipMap.insert(it, std::pair<int, QString >(i,line)); then it will run. 然后它将运行。 Since it's a seg fault I'm sure its a memory overflow or null pointer, but I'm just not sure what it is. 由于这是段错误,因此我确定它是内存溢出或空指针,但我不确定是什么。 Thanks for any help. 谢谢你的帮助。

I'm going to assume that your crash is due to these lines: 我将假设您的崩溃是由于以下几行引起的:

std::map<int, QString>::iterator it;
this->TipMap.insert(it, std::pair<int, QString >(i,line));

You're attempting to insert into a map using an invalid (uninitialized) iterator. 您正在尝试使用无效(未初始化)的迭代器插入地图。 Are you running this in debug or release? 您正在调试还是发布版本中运行它? You should get an assertion in debug. 您应该在调试中得到一个断言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM