• 6670阅读
  • 3回复

急!新手上路,用designer继承原类,编译报“没有合适的默认构造函数可用”(贴源码) [复制链接]

上一主题 下一主题
离线dec31
 
只看楼主 倒序阅读 楼主  发表于: 2009-07-19
我用designer制作相应的ui,然后转为*.h文件,在些基础之上,继承该类,最终是想在此desinger的对话框上点击一命令按钮后再弹出一个对话框。但编译后,提示“没有合适的默认构造函数可用",请各位看官,过客,高手指点

源码如下:

(1)GotoCellDialog.h
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

#include <QDialog>
//#include "Ui_GotoCellDialog.h"

namespace Ui
{
  class GotoCellDialog;
}

class GotoCellDialog :public QDialog
{
    Q_OBJECT
public:
    GotoCellDialog(QWidget *parent =0);
    ~GotoCellDialog();
private:
     Ui::GotoCellDialog *ui;
};

#endif

(2)GotoCellDialog.cpp
#include <QMessageBox>
#include "GotoCellDialog.h"
#include "Ui_GotoCellDialog.h"
#include <QDialog>
//#include <QtGui>

GotoCellDialog::GotoCellDialog(QWidget *parent):QDialog(parent),ui(new Ui::GotoCellDialog)//注,------此句报错,无合适的构造函数可用
{
   ui->setupUi(this);
   //ui->retranslateUi(this);
   //connect(m_pbtest, SIGNAL(clicked()), this, SLOT(OnTest()));
  }
GotoCellDialog::~GotoCellDialog()
{
    delete ui;
}
/*
void GotoCellDialog::OnTest()
{
     QMessageBox::about(this,tr("hello"),tr("hello"));
}*/

(3)Ui_GotoCellDialog.h


#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

#include <QVariant>
#include <QAction>
#include <QApplication>
#include <QButtonGroup>
#include <QComboBox>
#include <QDialog>
#include <QHeaderView>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>

QT_BEGIN_NAMESPACE


class Ui_GotoCellDialog
{
public:
    QLabel *label;

    QLabel *label_2;
    QTextEdit *m_a1;
    QTextEdit *m_a2;
    QTextEdit *m_a3;
    QLabel *label_4;
    QLabel *label_3;
    QComboBox *m_cb1;
    QComboBox *m_cb2;
    QPushButton *m_pbtest;
    QPushButton *m_pb1;
    QPushButton *m_pb2;
    QPushButton *m_pb3;
    QPushButton *m_pb4;
       QPushButton *m_pbreturn;

    void setupUi(QDialog *GotoCellDialog)
    {
        if (GotoCellDialog->objectName().isEmpty())
            GotoCellDialog->setObjectName(QString::fromUtf8("GotoCellDialog"));
        GotoCellDialog->resize(509, 359);
        label = new QLabel(GotoCellDialog);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(20, 30, 41, 16));
       ...
       ...
       ...

        retranslateUi(GotoCellDialog);
        QObject::connect(m_pbreturn, SIGNAL(clicked()), GotoCellDialog, SLOT(accept()));

        QMetaObject::connectSlotsByName(GotoCellDialog);
    } // setupUi

    void retranslateUi(QDialog *GotoCellDialog)
    {
        GotoCellDialog->setWindowTitle(QApplication::translate("GotoCellDialog", "Dialog", 0, QApplication::UnicodeUTF8));
     ...
     ...
     ...
        Q_UNUSED(GotoCellDialog);
    } // retranslateUi

};

namespace Ui {
  class GotoCellDialog: public Ui_GotoCellDialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_GotoCellDialog_H

编译后报错:
正在编译...
GotoCellDialog.cpp
.\GotoCellDialog.cpp(7) : error C2512: “Ui::GotoCellDialog”: 没有合适的默认构造函数可用
离线verytoy
只看该作者 1楼 发表于: 2009-07-19
Ui_GotoCellDialog.h
GotoCellDialog.h
用了同一个宏:
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
离线dec31
只看该作者 2楼 发表于: 2009-07-19
verytoy ,非常感谢你的帮助。

是哪块用了同一个宏,请明示,谢谢

是3)Ui_GotoCellDialog.h中的
“#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H ”这块吗,怎么改,我刚上路,请指点,谢谢
离线verytoy
只看该作者 3楼 发表于: 2009-07-20
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
是用来防止头文件被多次#include用的,比如:
---a.h:--------- ---
#ifndef A_H
#define A_H
......
#endif


----b.h:------------
#ifndef B_H
#define B_H
#include "a.h"
......
#endif

---main.cpp:--------
#include "a.h"
#include "b.h"
........

在编译main.cpp时首先include a.h
看A_H定义了没有,发现没有定义,就定义A_H,把#ifndef 和#endif之间的内容全部include
再include b.h 因为B_H没有定义,同样把#ifndef 和#endif之间的内容全部include
但是b.h里面有一个#include "a.h",它要再include一个a.h.这时a.h的第一句"ifndef A_H"不为真,因为前面定义过了,
所以a.h不会被包含两次 ,可以减少编译时间

同样原理,
你的程序中
Ui_GotoCellDialog.h ,GotoCellDialog.h 使用了相同的宏.
在GotoCellDialog.cpp中
#include "GotoCellDialog.h"
定义了一个GOTOCELLDIALOG_H(#define GOTOCELLDIALOG_H)
接着:
#include "Ui_GotoCellDialog.h"
判断"ifndef GOTOCELLDIALOG_H"自然为假,
所以Ui_GotoCellDialog.h等于没有被include进去
只要把两个文件的 #ifndef #define 后面的字段改为不同就行,
啰嗦半天,不知道说清楚没有,呵呵
[ 此帖被verytoy在2009-07-20 01:08重新编辑 ]
快速回复
限100 字节
 
上一个 下一个