【问题标题】:QMutableHashIterator - no appropriate default constructorQMutableHashIterator - 没有合适的默认构造函数
【发布时间】:2017-03-13 18:17:18
【问题描述】:

我正在尝试将数据传递到名为“字典”的哈希中,我想我会使用 QMutableHashIterator 来遍历哈希并向其中添加值,但是,我不断收到此错误,但我不知道如何解决这个问题。我看过其他有类似错误的问题,但没有一个真的对我有帮助。所以我想我会问,有人可以帮我解决这个错误:

mainwindow.cpp:7: error: C2512: 'QMutableHashIterator<QString,QString>' : no appropriate default constructor available

这是我的代码:

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>

namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

private slots:
  void on_pushButton_clicked();

private:
  Ui::MainWindow *ui;
  QHash<QString, QString> dictionary;
  QMutableHashIterator<QString, QString> i;
};

主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

  const QString content = "word";
  i = dictionary;
  while(i.hasNext())
  {
    i.next();
    i.setValue(content);
  }
}

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

void MainWindow::on_pushButton_clicked()
{
  QString word = "dog";

  while(i.findNext(word))
  {
    QMessageBox::information(this,tr("Word Has Been found"),
                             word);
  }
}

提前致谢!

【问题讨论】:

  • i 必须是会员吗?如果是,我们在哪里初始化数据成员?无论如何,我认为构造函数中的循环不会运行。

标签: c++ qt iterator qhash


【解决方案1】:

您必须使用要遍历的QHash 初始化i。见QMutableHashIterator documentation

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
i(dictionary) // here
{
  // ...
}

或者简单地说,如果您的解决方案的逻辑允许,每次您想将迭代器用作成员变量时都创建迭代器。

【讨论】:

  • 很高兴为您提供帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 2011-12-22
  • 2016-01-22
  • 2014-06-23
相关资源
最近更新 更多