c++ - 在 B 类中初始化 A 类(带有引用成员)

标签 c++ qt class oop

我正在尝试制作一个简单的货币转换器,但我在设计用于计算的类(class)时遇到了一些问题。 我想在我的 MainWindow 中创建一个此类的对象,以使用 total 变量在我的 appi 表单上的小部件中显示结果。

成员函数还不全在这里,但我遇到的问题是我得到了一个 “计算器”:mainwindow.cpp 中没有合适的默认构造函数可用错误

虽然我知道这意味着什么,但我不知道如何避免(解决)这个问题,因为我需要 map 上的引用来获得所需的兑换率。

.cpp

#include <QObject>
#include <QString>
#include <QDebug>
#include <ui_mainwindow.h>

class Calculator
{
public:
    explicit Calculator(QMap<QString,double> &currency_map);
    void multiply(double x, double y);
    void getValues(QString strFrom, QString strTo);

private:
    double total, firstCurr, secondCurr;
    QMap<QString,double> &map;
};

.h

#include "calculator.h"

Calculator::Calculator(QMap<QString,double> &currency_map):map(currency_map)
{
    total = 0;
    firstCurr = 0;
    secondCurr= 0;
}

void Calculator::getValues(QString strFrom, QString strTo)
{
      QMapIterator<QString, double> i(map);
    while(i.hasNext())
        {
            if(i.key() == strFrom)
                firstCurr=i.value();
            if(i.key() == strTo)
                secondCurr = i.value();
    }
}

void Calculator::multiply(double x, double y)
{
    total = x * y;
}

现在我试图在我的 MainWindow 类中创建该类的对象:

#include <QMainWindow>
#include <ui_mainwindow.h>
#include "calculator.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QMap<QString,double> &currency_map, QWidget *parent = 0);

    ~MainWindow();

private:
    Ui::MainWindow *ui;
    parser currency_parser;
    Calculator calc; // error

};

主窗口.h

MainWindow::MainWindow(QMap<QString, double> &currency_map, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->from_Combox->addItems(currency_parser.currency_list);
    ui->to_Combox->addItems(currency_parser.currency_list);
}

背后的想法是,我在我的 main 中有一个 Map,我在其中保存了所有需要的数据,并将该 map 传递给使用它的类。

我会发布我的主要内容,虽然我不确定是否需要它

#include "downloader.h"
#include "mainwindow.h"
#include "parser.h"
#include "calculator.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMap<QString,double> currency_map;

    downloader d;
    d.Do_download();

    parser p;
    p.read_line(currency_map);

    MainWindow w(currency_map);
    w.show();

    return a.exec();
};

最佳答案

发生错误,因为您没有在 MainWindow 构造函数成员初始化列表中为类 Calculator 指定适当的构造函数(编译器随后希望使用默认构造函数):

MainWindow::MainWindow(QMap<QString, double> &currency_map, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, calc(currency_map) // <<<<<<<<<<<<<<
{
    // ...
}

关于c++ - 在 B 类中初始化 A 类(带有引用成员),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388355/

相关文章:

C++ 嵌套类错误 "cannot convert ... in assignment"

Java 类无法正确交互

c++ - 在两个独立的 opengl 窗口中渲染一个独特的视频流

c++ - CodeLite 中的graphics.h?

c++ - 统一 SWI Prolog 中的动态谓词

c++ - 通过带有成员函数指针的 QHash 调用成员函数的正确方法

c++ - getline() 不提取定界符吗?

qt - 添加对象到 QListWidget

c++ - QSqlQuery 插入完整结构而不是单个字段?

class - 将资源嵌入可重用 MFC 类的正确方法是什么?