Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

No Appropriate Default Constructor -- Help Please??



  • Hello QtProject again,

    I've been posting here for the past month now as I have been working on a school project for the last month asking for help(which I greatly appreciate the help from you fine folk). Anyways, to the error:

    @error: C2512: 'Ui::GuiDialog' : no appropriate default constructor available

    error: C1903: unable to recover from previous error(s); stopping compilation@

    Here is my header file and snippet of my code:

    GuiDialog.h
    @#ifndef GUIDIALOG_H
    #define GUIDIALOG_H

    #include <QObject>
    #include <QtGui>
    #include <QDialog>
    #include <QLabel>
    #include "Dealer.h"
    #include "Player.h"
    #include "GameModel.h"

    namespace Ui
    {
    class GuiDialog;
    }

    class GuiDialog: public QDialog
    {
    Q_OBJECT

    public:
    GuiDialog(QWidget *parent=0);
    GuiDialog(QWidget *, GameModel *);
    ~GuiDialog();
    public slots:
    void updateCard(int, int);
    void newScore(int);
    void endGame();
    void playerWon();
    void dealerWon();
    void tie();
    void updatePlayerThirdCard();
    void updateDealerThirdCard();
    private:
    GameModel *model;
    Ui::GuiDialog *ui;
    QLabel *playerCards, *dealerCards, *displayWinner;
    QPushButton *betButton;
    QLCDNumber *number;
    int score;

    };

    #endif // GUIDIALOG_H@

    GuiDialog.cpp
    @#include "GuiDialog.h"
    #include "GameModel.h"
    #include <QDialog>
    #include <QtGui>
    #include <QPixmap>
    #include <QLabel>
    #include <QtGlobal>

    /*****************************************
    /* This class will implement the changes
    /* taking place in the GUI such as updating
    /* the cards being displayed
    /*****************************************/

    GuiDialog::GuiDialog(QWidget *parent) : QDialog(parent), ui(new Ui::GuiDialog) //<----error takes me to this line
    {
    ui->setupUi(this);
    }

    GuiDialog::GuiDialog(QWidget *parent, GameModel *model): QDialog(parent), ui(new Ui::GuiDialog)
    {
    betButton = new QPushButton(tr("&Bet"));

    QSpinBox *spinBox = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    spinBox->setRange(0, 100000);
    slider->setRange(0, 100000);
    
    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     spinBox, SLOT(setValue(int)));
    
    /**/@
    

    I've searched at the Qt-Project forums for help as well as google for solutions. Sadly, I could not really find anything here and google only showed me results of C++ programs not involving Qt. I understand that the compiler is not reading my default constructor that's why it refuses to compile, but I thought that first set of lines in GuiDialog.cpp(where the error takes me) is the default constructor? Granted, I am a student barely learning this and I merely copied the syntax of brand new "MainWindow" project in hopes that it would work. But apart from that, I really want to know what constitutes a default constructor such that the first lines of my GuiDialog.cpp do not make it the default constructor as I thought it would be?

    Thank you for your help and time.



  • This code at line 23 of GuiDialog.h seems like incomplete
    @GuiDialog(QWidget *, GameModel *);@

    try to change it this way:
    @GuiDialog(QWidget *parent, GameModel *model);@



  • Code_ReaQtor, I think it is okay to leave out the argument names in the declaration.

    AngelMacias, the error message would indicate that Ui::GuiDialog has no appropriate default constructor. But it seems you nowhere include the header file where that class is declared! Sure, in your GuiDialog.h you have a forward declaration of Ui::GuiDialog, but an actual declaration is missing! Which explains why that class cannot be instantiated! Normally you would have a Qt Designer .ui file that you compile with the "UIC":http://doc.qt.digia.com/qt/uic.html in order to generate the header .h file with the declaration of Ui::GuiDialog. This you would then include in your GuiDialog.cpp file, in addition to your own hand-written .h file! Note that GuiDialog and Ui::GuiDialog are two separate classes. The former is your own class, the latter is the class generated by Qt Designer/UIC.

    Have a look here:
    http://doc.qt.digia.com/qt/designer-using-a-ui-file.html

    BTW: What you are using is "The Single Inheritance Approach" with "Using a Pointer Member Variable".



  • I found what you were referring to MuldeR.

    @/********************************************************************************
    ** Form generated from reading UI file 'GuiDialog.ui'
    **
    ** Created: Fri Dec 7 16:13:14 2012
    ** by: Qt User Interface Compiler version 4.8.1
    **
    ** WARNING! All changes made in this file will be lost when recompiling UI file!
    ********************************************************************************/

    #ifndef UI_GUIDIALOG_H
    #define UI_GUIDIALOG_H

    #include <QtCore/QVariant>
    #include <QtGui/QAction>
    #include <QtGui/QApplication>
    #include <QtGui/QButtonGroup>
    #include <QtGui/QDialog>
    #include <QtGui/QHeaderView>
    #include <QtGui/QLCDNumber>
    #include <QtGui/QLabel>
    #include <QtGui/QPushButton>
    #include <QtGui/QSlider>
    #include <QtGui/QSpinBox>

    QT_BEGIN_NAMESPACE

    class Ui_Dialog
    {
    public:
    QSlider *slider;
    QSpinBox *spinBox;
    QPushButton *betButton;
    QLCDNumber *number;
    QLabel *playerCards;

    void setupUi(QDialog *Dialog)
    {
        if (Dialog->objectName().isEmpty())
            Dialog->setObjectName(QString::fromUtf8("Dialog"));
        Dialog->resize(779, 462);
        Dialog->setStyleSheet(QString::fromUtf8("GuiDialog { background-image: url(:/images/table.png) };"));
        slider = new QSlider(Dialog);
        slider->setObjectName(QString::fromUtf8("slider"));
        slider->setGeometry(QRect(200, 400, 491, 21));
        slider->setOrientation(Qt::Horizontal);
        spinBox = new QSpinBox(Dialog);
        spinBox->setObjectName(QString::fromUtf8("spinBox"));
        spinBox->setGeometry(QRect(120, 400, 61, 22));
        betButton = new QPushButton(Dialog);
        betButton->setObjectName(QString::fromUtf8("betButton"));
        betButton->setGeometry(QRect(330, 360, 91, 31));
        number = new QLCDNumber(Dialog);
        number->setObjectName(QString::fromUtf8("number"));
        number->setGeometry(QRect(100, 350, 91, 41));
        number->setFrameShadow(QFrame::Sunken);
        number->setDigitCount(5);
        number->setProperty("intValue", QVariant(500));
        playerCards = new QLabel(Dialog);
        playerCards->setObjectName(QString::fromUtf8("playerCards"));
        playerCards->setGeometry(QRect(220, 190, 351, 131));
        playerCards->setPixmap(QPixmap(QString::fromUtf8(":/cards/Cards/back.jpg")));
    
        retranslateUi(Dialog);
        QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
        QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));
    
        QMetaObject::connectSlotsByName(Dialog);
    } // setupUi
    
    void retranslateUi(QDialog *Dialog)
    {
        Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
        betButton->setText(QApplication::translate("Dialog", "Bet", 0, QApplication::UnicodeUTF8));
        playerCards->setText(QString());
    } // retranslateUi
    

    };

    namespace Ui {
    class Dialog: public Ui_Dialog {};
    } // namespace Ui

    QT_END_NAMESPACE

    #endif // UI_GUIDIALOG_H@

    So I tried doing this...

    @namespace Ui {
    class Dialog: public Ui_Dialog {};

    class GuiDialog : public Ui_Dialog { };
    

    }@

    The compiler did not give me any errors regarding the constructor anymore, but I just want to make sure I'm doing it right.



  • I cannot follow your code... ???

    Normally your header file (.h), the one you write yourself, looks like:
    @#ifndef GUIDIALOG_H
    #define GUIDIALOG_H

    /* more includes you may need */

    //Forward declaration of Ui::GuiDialog
    namespace Ui
    {
    class GuiDialog;
    }

    //Your own class that uses the Ui::GuiDialog class
    class GuiDialog: public QDialog
    {
    Q_OBJECT

    public:
    GuiDialog(QWidget *, GameModel *);
    ~GuiDialog();

    /* ..... */
    

    private:
    Ui::GuiDialog *ui; //Pointer member-variable for GUI

    /* ..... */
    

    };

    #endif // GUIDIALOG_H@

    And in your code file (.cpp) you would do:
    @#include "GuiDialog.h" //Your own header file from above
    #include "UIC_GuiDialog.h" //The header generated by the UIC (for Ui::GuiDialog)

    /* more includes you may need */

    //Constructor
    GuiDialog::GuiDialog(QWidget *parent, GameModel *model) :
    QDialog(parent), ui(new Ui::GuiDialog)
    {
    ui->setupUi(this);
    }

    //Destructor
    GuiDialog::~GuiDialog(void)
    {
    delete ui;
    }

    /* ........... */
    @

    And remember: Never try to edit the .h file that UIC has generated. Your changes would be lost at the next re-compile. UIC re-generates the .h file from the .ui file. Just use the UIC-generated .h file as it pops out of UIC.

    Also: In the above code I assumed the UIC-generated file is at "UIC_GuiDialog.h". Adjust that path as required!



  • Hello MuldeR,

    due to the topic i get the same error C2512 by using Ui::SettingsDialog in combination with QSerialPort terminal example.

    I am using Qt 4.8.4 and could compile the master blocking example, so i guess that i install QSerialPort right. Do you have an idea or hint whats wrong?
    @
    #ifndef SETTINGSDIALOG_H
    #define SETTINGSDIALOG_H
    #include <QDialog>
    #include <QSettings>
    #include <QtSerialPort/QSerialPort>
    QT_USE_NAMESPACE
    namespace Ui {
    class SettingsDialog;
    }
    class QIntValidator;
    class SettingsDialog : public QDialog
    {
    Q_OBJECT

    public:
    struct Settings {
    QString name;
    qint32 baudRate;
    QString stringBaudRate;
    QSerialPort::DataBits dataBits;
    QString stringDataBits;
    QSerialPort::Parity parity;
    QString stringParity;
    QSerialPort::StopBits stopBits;
    QString stringStopBits;
    QSerialPort::FlowControl flowControl;
    QString stringFlowControl;
    bool localEchoEnabled;
    };

    SettingsDialog(QWidget *parent = 0);  //explicit
    ~SettingsDialog();
    
    Settings settings() const;
    

    private slots:
    void showPortInfo(int idx);
    void apply();
    void checkCustomBaudRatePolicy(int idx);

    private:
    void fillPortsParameters();
    void fillPortsInfo();
    void updateSettings();

    private:
    Ui::SettingsDialog *ui;
    Settings currentSettings;
    QIntValidator *intValidator;
    };

    #endif // SETTINGSDIALOG_H
    @

    @#include "settingsdialog.h"
    #include "ui_settingsdialog.h"

    #include <QtSerialPort/QSerialPortInfo>
    #include <QIntValidator>
    #include <QLineEdit>

    QT_USE_NAMESPACE

    SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsDialog) // here C2512
    {
    ui->setupUi(this);

    intValidator = new QIntValidator(0, 4000000, this);
    
    ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert);
    
    connect(ui->applyButton, SIGNAL(clicked()),
            this, SLOT(apply()));
    connect(ui->serialPortInfoListBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(showPortInfo(int)));
    connect(ui->baudRateBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(checkCustomBaudRatePolicy(int)));
    
    fillPortsParameters();
    fillPortsInfo();
    
    updateSettings();
    

    }

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

    SettingsDialog::Settings SettingsDialog::settings() const
    {
    return currentSettings;
    }
    // continued
    //
    }@

    Thank you for your help.



  • Check your "ui_settingsdialog.h" file, which I assume was generated by UIC.

    Is the file in place? And does it really define a class Ui::SettingsDialog? If so, how does the constructor of that class look?



  • Hello MuldeR,

    u assumed right, it was not generatet the ui_settingsdialog.h. I changed the constructor name to Dialog and did not changed the class name into the dialog.ui. It works great now.

    @
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>Dialog</class> // here
    <widget class="QDialog" name="Dialog">
    <property name="geometry">
    @

    Thank you for your reply. It is going more and more clear for me.


Log in to reply