Qt 没有合适的默认构造函数可用
本人最近写一个demo,程序有两个类:bdy_1和HttpGet。bdy_1中有一个成员是HttpGet的对象。下面是bdy_1和HttpGet的定义和构造函数:
HttpGet的定义:
class HttpGet : public QObject
{
Q_OBJECT
public:
HttpGet(QObject *parent);
//HttpGet();
~HttpGet();
bool downloadFile(QUrl&);
signals:
void done();
private slots:
void httpRequestFinished(int requestId, bool error);
void readResponseHeader(const QHttpResponseHeader &responseHeader);
void updateDateReadProgress(int bytesRead, int totalBytes);
void httpDone(bool error);
private:
QHttp *http;
QFile *file;
int httpGetId;
bool httpRequestAborted;
QString fileName;
};
HttpGet的构造函数:
HttpGet::HttpGet(QObject *parent)
: QObject(parent)
{
http = new QHttp(this);
connect(http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
connect(http, SIGNAL(requestFinished(int ,bool)), this, SLOT(httpRequestFinished(int, bool)));
connect(http, SIGNAL(dataReadProgress(int,int)), this, SLOT(updateDataReadProgress(int,int)));
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(readResponseHeader(const QHttpResponseHeader&)));
}
bdy_1的定义:
class bdy_1 : public QMainWindow
{
Q_OBJECT
public:
bdy_1(QWidget *parent = 0, Qt::WFlags flags = 0);
~bdy_1();
public slots:
void load();
private:
Ui::bdy_1Class ui;
public:
HttpGet httpGet;
};
bdy_1的构造函数:
bdy_1::bdy_1(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
httpGet = new HttpGet(this);
ui.setupUi(this);
connect(ui.downloadButton, SIGNAL(clicked()), this, SLOT(load()));
}
这个程序在编译时,会报错:
error C2512: “HttpGet”: 没有合适的默认构造函数可用
本人有两个问题不明白,第一,为什么会报错,类HttpGet确实没有构造函数,但是我在bdy_1的构造函数里面实例化httpGet时没有使用默认构造函数,而是用了带参的构造函数:
httpGet = new HttpGet(this);
第二,类bdy_1也没有默认构造函数(默认构造函数应该是不带参数的,而这里的构造函数带参)但是主程序main.c在运行后却调用了bdy_1这个唯一拥有的构造函数,感觉就是把这个构造函数当成默认构造函数了,以下是main.c:
#include "bdy_1.h"
#include <QtGui/QApplication>
#include <qtextcodec.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec *codec = QTextCodec::codecForName("System"); //获取系统编码
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
bdy_1 w;
w.show();
w.httpGet.downloadFile(QUrl("http://2.xp85.com/soft/小马激活工具_xp85.com.zip"));
return a.exec();
}
qt 构造函数 error:C2512
[解决办法]
您这是大错呀
public:
HttpGet httpGet; //这是定义了一个HttpGet类对象的实例.这里httpGet初始化,要调用默认构造函数呀,你在httpGet类中定义了一个带参的构造函数了,编译器就不会再提供默认的构造函数了,你得自己加上
构造函数里您又这么写:
httpGet = new HttpGet(this); //new 返回的是一个HttpGet类指针呀
这类型都不相符呀