问题
I am trying to implement a basic http connection http://developer.nokia.com/community/wiki/Creating_an_HTTP_network_request_in_Qt into Qt but I have difficultly implementing the slot. I am a Qt novice.
C:\Qt5\Tools\QtCreator\bin\miniHTTP\main.cpp:10: error: request for member 'Test' in 'mTest', which is of non-class type 'coreEng()' mTest.Test();
//main.cpp
#include <QCoreApplication>
#include <coreeng.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
coreEng mTest;
mTest.Test();
return a.exec();
}
//coreeng.h
#ifndef COREENG_H
#define COREENG_H
#include <QDebug>
#include <QObject>
#include <QNetworkAccessManager>
class coreEng : public QObject
{
Q_OBJECT
public:
explicit coreEng(QObject *parent = 0);
void Test();
private slots:
public slots:
void connect();
void url();
void finishedSlot();
private:
QNetworkAccessManager* nam;
};
#endif // COREENG_H
//coreeng.cpp
#include "coreeng.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>
coreEng::coreEng(QNetworkReply* parent) :
QObject(parent)
{
}
void coreEng::Test();
void coreEng::connect(){
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
}
void coreEng::url(){
QUrl url("http://www.forum.nokia.wiki");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}
void coreEng::finishedSlot(QNetworkReply* reply){
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
QImage pic = imageReader.read();
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
}
else
{
}
delete reply;
}
回答1:
One problem is you are running into the most vexing parse: http://en.wikipedia.org/wiki/Most_vexing_parse
Most vexing parse
Change
coreEng mTest();
to
coreEng mTest;
A second problem is your constructor in coreeng.cpp has the wrong signature
Change:
coreEng::coreEng(QNetworkReply*) :
QObject(parent)
to
coreEng::coreEng(QObject* parent) :
QObject(parent)
回答2:
You have declared void Test();
as a member function in the header file and have not implemented it in the cpp file. You should implement it :
void coreEng::Test()
{
...
}
回答3:
In addition to other problems already described, your implementation of finished slot doesn't have the same signature of as the function definition. In your .h file you have:
void finishedSlot();
Whereas your .cpp has:
void finishedSlot(QNetworkReply *reply) {
/*code here*/
}
So to summarise:
Add an implementation of your test function:
void coreEng::Test() { /code here/ }
Fix the signature of your constructor so that .cpp and .h files agree:
coreEng::coreEng(QObject* parent) : QObject(parent) { /code here/ }
Fix the signature of your finishedSlot so that .cpp and .h files agree:
void finishedSlot(QNetworkReply *reply);
These changes will at least get you to the stage that you're compiling successfully.
来源:https://stackoverflow.com/questions/24168779/slot-error-using-qnetworkaccessmanager