Multiple definitions of slots / QNetwork implementation
-
Hello,
I am currently trying to send HTTP requests via QNetwork.
However, I have a "multiple definitions" error in the slots used to receive responses.
That's my code:
main.cpp
#include <QCoreApplication> #include "Httpclient.h" #include <QTextStream> #include <iostream> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); HttpClient *client = new HttpClient(); client->HttpRequest(); std::cout << "End program" << std::endl; return a.exec(); }
HttpClient.cpp
#include "Httpclient.h" #include <QTextCodec> #include <iostream> HttpClient::HttpClient() { QNetworkAccessManager *mgr = new QNetworkAccessManager(this); QNetworkRequest req; req.setUrl(QUrl("http://google.com")); req.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"); this->resp = mgr->get(req); connect(this->resp, SIGNAL(readyRead()), this, SLOT(slotReadyRead())); connect(this->resp, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError))); connect(this->resp, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>))); } void HttpClient::HttpRequest() { } void HttpClient::slotReadyRead() { std::vector<char> buf; qint64 chunk; QString allbuf; while(resp->bytesAvailable() > 0) { chunk = this->resp->bytesAvailable(); if(chunk > 4096) chunk = 4096; buf.resize(chunk + 1); memset(& buf[0], 0, chunk + 1); if(chunk != resp->read(& buf[0], chunk)) { qDebug("-> read error"); } else { qDebug("-> read ok"); } allbuf += & buf[0]; } std::cout << allbuf.toUtf8().constData() << std::endl; } void HttpClient::slotError(QNetworkReply::NetworkError) { } void HttpClient::slotSslErrors(QList<QSslError>) { }
HttpClient.h
#ifndef HTTPCLIENT_H #define HTTPCLIENT_H #include <QtNetwork/QNetworkAccessManager> #include <QNetworkReply> #include <QObject> class HttpClient : public QObject { Q_OBJECT public: HttpClient(); void HttpRequest(); private: QNetworkAccessManager *manager; QNetworkRequest request; QNetworkReply *resp; signals: void slotReadyRead(); void slotError(QNetworkReply::NetworkError); void slotSslErrors(QList<QSslError> ); }; #endif // HTTPCLIENT_H
Thank you for your help.
-
Hi
What does the error say exactly?
normally it points to a symbol.
Is it the HttpClient::slotReadyRead() ?I would do.
Delete build folder and rebuild all.
If error is still there.
Then check the .pro file if any files is added twice. -
Sorry I forgot to put the error....
/usr/bin/ld : moc_Httpclient.o : dans la fonction « HttpClient::slotReadyRead() » : /home/ludo/Projets_epitech/build-TestHttp-Desktop_Qt_5_13_0_GCC_64bit-Debug/moc_Httpclient.cpp:175 : définitions multiples de « HttpClient::slotReadyRead() »; Httpclient.o:/home/ludo/Projets_epitech/build-TestHttp-Desktop_Qt_5_13_0_GCC_64bit-Debug/../TestHttp/Httpclient.cpp:22 : défini pour la première fois ici /usr/bin/ld : moc_Httpclient.o : dans la fonction « HttpClient::slotError(QNetworkReply::NetworkError) » : /home/ludo/Projets_epitech/build-TestHttp-Desktop_Qt_5_13_0_GCC_64bit-Debug/moc_Httpclient.cpp:181 : définitions multiples de « HttpClient::slotError(QNetworkReply::NetworkError) »; Httpclient.o:/home/ludo/Projets_epitech/build-TestHttp-Desktop_Qt_5_13_0_GCC_64bit-Debug/../TestHttp/Httpclient.cpp:49 : défini pour la première fois ici /usr/bin/ld : moc_Httpclient.o : dans la fonction « HttpClient::slotSslErrors(QList<QSslError>) » : /home/ludo/Projets_epitech/build-TestHttp-Desktop_Qt_5_13_0_GCC_64bit-Debug/moc_Httpclient.cpp:188 : définitions multiples de « HttpClient::slotSslErrors(QList<QSslError>) »; Httpclient.o:/home/ludo/Projets_epitech/build-TestHttp-Desktop_Qt_5_13_0_GCC_64bit-Debug/../TestHttp/Httpclient.cpp:53 : défini pour la première fois ici collect2: error: ld returned 1 exit status make: *** [Makefile:265: TestHttp] Error 1
I still have the error when deleting the build folder
And no files added twice in my .pro file:
QT -= gui QT += network CONFIG += c++11 console CONFIG -= app_bundle # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ Httpclient.cpp \ main.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ Httpclient.h ``
-
Hi
Hmm it does seems fine.
Its the fullproject/code so i can put in a defualt project to check it out ?Also, you did try to delete the build folder completely ?
-
Hi
Im just blind :)
You have them listed under signals.
But its slots.
So when you list them as signals and then gives them body you get this error :)so
public slots:
void slotReadyRead();
void slotError(QNetworkReply::NetworkError);
void slotSslErrors(QList<QSslError> );fixes it.
(code runs and download something)End program -> read ok <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/">here</A>. </BODY></HTML>
-
Hi
I bumped you so you dont have to wait so long to reply.
Regarding the build folder and deleting it.
if you use Creator, you can right click and go there directly.
Then ctrl+a and then delete key :)
-
@rapishiny
Dont think about it. Shit happens.
I looked at it for several minutes after getting/reproducing same error before
i realized it. So it was slightly hard to detect as it was more logical than hard error. -
Hi,
Just in case, you are leaking QNetworkAccessManager objects in your HttpRequest method.