【问题标题】:no appropriate default constructor available but constructor is declared没有合适的默认构造函数可用,但声明了构造函数
【发布时间】:2013-03-13 17:14:39
【问题描述】:

我想从一个主窗口创建一个窗口,并向它发送一个指向我的主窗口的指针。我做了一次,我会再做一次,但是第二次,我总是在编译时出现这个错误“没有适当的默认构造函数可用”

主窗口:

#include "costsimulator.h"
#include "ui_costsimulator.h"

#include "stonepricewindow.h"

CostSimulator::CostSimulator(AionEnhancingSimulator *parent) : ui(new Ui::CostSimulator)
{
ui->setupUi(this);

parentPtr = parent;
stonePrice = createStonePrice();
connect(ui->aionEnhancingSimulator, SIGNAL(clicked()), this, SLOT(showAionEnhancingSimulatorWindow()));
connect(ui->stonePriceButton, SIGNAL(clicked()), this, SLOT(showStonePriceWindow()));
}

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

void CostSimulator::showAionEnhancingSimulatorWindow()
{
this->hide();
parentPtr->show();
}

QStringList *createStonePrice()
{
QStringList *tmp = new QStringList();

tmp->push_back(QString("80-30000000"));
return (tmp);
}

void CostSimulator::showStonePriceWindow()
{
StonePriceWindow *stonepricewindow = new StonePriceWindow(this);
stonepricewindow->show();
}

QStringList *CostSimulator::getStonePrice()
{
return (stonePrice);
}

和标题:

#ifndef COSTSIMULATOR_H
#define COSTSIMULATOR_H

#include <QDialog>

#include "aionenhancingsimulator.h"

namespace Ui {
class CostSimulator;
}

class CostSimulator : public QDialog
{
Q_OBJECT

public:
AionEnhancingSimulator *parentPtr;
explicit CostSimulator(AionEnhancingSimulator *parent);
~CostSimulator();

QStringList *stonePrice;

QStringList *createStonePrice();
QStringList *getStonePrice();
void showStonePriceWindow();

public slots:
void showAionEnhancingSimulatorWindow();

private:
Ui::CostSimulator *ui;
};

#endif // COSTSIMULATOR_H

以及导致问题的窗口:

#include "stonepricewindow.h"
#include "ui_stonepricewindow.h"
#include <QStringListModel>
#include <QStandardItemModel>
#include <QtGui>

StonePriceWindow::StonePriceWindow(CostSimulator *parent) : ui(new Ui::StonePriceWindow)
{
ui->setupUi(this);
displayStonePriceList(parent);
}

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

void StonePriceWindow::displayStonePriceList(CostSimulator *parent)
{
// To do
}

标题

#ifndef STONEPRICEWINDOW_H
#define STONEPRICEWINDOW_H

#include <QDialog>

#include "costsimulator.h"

namespace Ui {
class StonePriceWindow;
}

class StonePriceWindow : public QDialog
{
Q_OBJECT

public:
explicit StonePriceWindow(CostSimulator *parent = 0);
~StonePriceWindow();

void displayStonePriceList(CostSimulator *parent);

private:
Ui::StonePriceWindow *ui;
};

#endif // STONEPRICEWINDOW_H

如果我在 StonePriceWindow 的标头中有此“StonePriceWindow() {}”,则会出现以下错误:“指定了多个默认构造函数”并且始终显示“没有适当的等..”

感谢您的帮助,我不明白为什么。

【问题讨论】:

  • 您能否真正提供 exact 编译错误及其发生所在的行,而不是大量使用代码。
  • stonepricewindow.cpp:7: erreur : C2512: 'Ui::StonePriceWindow' : 没有合适的默认构造函数可用,它位于这一行: StonePriceWindow::StonePriceWindow(CostSimulator *parent) : ui(new Ui::StonePriceWindow) { ui->setupUi(this); displayStonePriceList(父); }
  • 对,所以有两个StonePriceWindow 类,一个在Ui 命名空间中,这就是导致问题的原因。我建议查看 ui_stonepricewindow.h
  • Ui::StonePriceWindow 是由 uic 生成的,所以应该是正确的 - 它也应该有一个编译器创建的构造函数。您是否尝试过执行完全清理和重建?
  • 是的,我尝试右键单击并清理项目,但什么也没做...

标签: c++ qt window parent-child


【解决方案1】:

抱歉,我无法对最佳答案添加评论..

我也遇到了类似的问题,似乎我创建的代码的类名与 Qt .ui 文件中对话框的 objectName 属性值不匹配。

我在 QtDesigner 中打开了 .ui 文件,并将 objectName 属性值更改为我在代码中使用的类名。之后编译很顺利。

【讨论】:

    【解决方案2】:

    尝试添加 StonePriceWindow(){} 并删除其他构造函数的默认参数。

    【讨论】:

    • 感谢您的帮助。但是有完全相同的问题`public: StonePriceWindow(){} StonePriceWindow(CostSimulator *parent); ~StonePriceWindow();` 并且总是 stonepricewindow.cpp:7: erreur : C2512: 'Ui::StonePriceWindow' : 没有合适的默认构造函数可用
    【解决方案3】:

    当你写代码时

    StonePriceWindow x;
    

    编译器如何知道你调用的是无参数构造函数还是另一个默认值为parent的构造函数?

    您需要删除另一个,或从中删除默认值。

    【讨论】:

    • 在提供的代码中写在哪里?而StonePriceWindow 类没有无参数构造函数。
    • 这不是写在代码中的,但是编译器需要用功能相同的代码在某处实例化对象。 StonePriceWindow 没有无参数 ctor,所以他添加了一个 - 请参阅问题的最后一句:“如果我在标题中添加了这个 'StonePriceWindow() {}'...”
    • new Ui::StonePriceWindowStonePriceWindow ctor 实现中调用是“功能上等于”StonePriceWindow x
    • 我了解正在发生的事情,但 OP 显然没有,因此提供一个与问题中的代码没有明显关系的相当神秘的答案 - 对任何人都没有帮助。编辑您的答案,以明确为什么 OP 从发布的代码中得到此错误,我会支持您。
    • 我调用我的ctor的唯一时刻是StonePriceWindow *stonepricewindow = new StonePriceWindow(this);,如果我在标题中添加StonePriceWindow(){}并删除我的实际ctor的默认值,我有同样的错误。
    【解决方案4】:

    我发现了问题。 在 StonePrinceWindow.ui 中,有一个过时的 UI 名称,因此自动生成的 ui_stonepricewindow 保留过时的名称(即使在清理之后),编译器永远找不到 ctor。

    【讨论】:

    • 您解释了问题,但没有解释解决方案。你是怎么解决这个问题的?我也有同样的问题。
    【解决方案5】:

    解决办法是换字符串

    #ifndef COSTSIMULATOR_H
    #define COSTSIMULATOR_H
    

    在 costsimulator.h 和 ui_costsimulator.h 中的任一个文件中

    Qt 在自动生成的 ui_costsimulator.h 中放置相同的 #ifndef #define 表达式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2013-03-20
      • 1970-01-01
      相关资源
      最近更新 更多