MrBlue
MrBlue

Reputation: 37

no appropriate default constructor available but constructor is declared

I wanted to create a window from a mainwindow, and send it a pointer to my main window. I did it one time, and i would do it again, but the second time, I always have this error at compilation "no appropriate default constructor available"

The main window:

#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);
}

and the header:

#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

and the window that cause the problem:

#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
}

the header

#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

if I had this " StonePriceWindow() {}" to the header of StonePriceWindow, I have the following error: "multiple default constructors specified" and always the "no appropriate etc .."

Thanks for any help, I can't understand why.

Upvotes: 0

Views: 3243

Answers (5)

whatever
whatever

Reputation: 11

The solution is to change the strings

#ifndef COSTSIMULATOR_H
#define COSTSIMULATOR_H

in either of the files costsimulator.h and ui_costsimulator.h

Qt places the same #ifndef #define expression in auto generated ui_costsimulator.h

Upvotes: 0

dutchdukes
dutchdukes

Reputation: 395

Sorry, but I could not add a comment on the best answer..

I had a similar problem too, it appeared that there was a mismatch between the classname of the code I created and the dialogs' objectName property value in the Qt .ui file.

I opened the .ui file in QtDesigner, and changed the objectName property value to the classname I had used in my code. Afterwards compilation was going fine.

Upvotes: 1

MrBlue
MrBlue

Reputation: 37

I found the problem. In the StonePrinceWindow.ui, there was an obsolete name of the UI, so the auto-generated ui_stonepricewindow keep the obsolete name (even after a clean) and the compiler never find the ctor.

Upvotes: 0

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14581

When you write code like

StonePriceWindow x;

how does compiler know whether you call the parameterless constructor or another one with default value for parent?

You need to either remove the other one, or remove the default value from it.

Upvotes: 0

John Bandela
John Bandela

Reputation: 2436

Try adding StonePriceWindow(){} and removing the default parameter for the other constructor.

Upvotes: 0

Related Questions