When adding code
JSParse * jsp = new JSParse ();
jsp- & gt; sendRequest ();
In main.cpp
# include "mainwindow.h"
#include "sql.h"
#include & lt; QApplication & gt;
#include "jsparse.h"
int main (int argc, char * argv [])
{
Sql * sql = new Sql ();
QApplication a (argc, argv);
MainWindow w;
w.show ();
w.updateUsers (sql- & gt; users);
JSParse * jsp = new JSParse ();
jsp- & gt; sendRequest ();
return a.exec ();
}
Throws errors:
main.obj: -1: error: LNK2019: unresolved external symbol "public: __cdecl JSParse :: JSParse (void ) "(?? 0JSParse @@ QEAA @ XZ) referenced in function main
main.obj: -1: error: LNK2019: unresolved external symbol "public: void __cdecl JSParse :: sendRequest (void)" (? sendRequest @ JSParse @@ QEAAXXZ) referenced in function main
debug \ project01.exe: -1: error: LNK1120: 2 unresolved externals
The jsparse.h file
# ifndef JSPARSE_H
#define JSPARSE_H
class JSParse
{
public:
JSParse ();
~ JSParse ();
void sendRequest ();
};
#endif // JSPARSE_H
jsparse.cpp file
# include "jsparse.h"
#include & lt; QDebug & gt;
#include & lt; QtWebKitWidgets / QWebFrame & gt;
#include & lt; QtWebKitWidgets / QWebPage & gt;
#include & lt; QtWebKitWidgets / QWebView & gt;
#include & lt; QNetworkAccessManager & gt;
#include & lt; QNetworkRequest & gt;
#include & lt; QNetworkReply & gt;
#include & lt; QUrl & gt;
#include & lt; QUrlQuery & gt;
#include & lt; QWebSettings & gt;
#include & lt; QVariant & gt;
#include & lt; QJsonValue & gt;
#include & lt; QJsonDocument & gt;
#include & lt; QJsonObject & gt;
#include & lt; QVariantMap & gt;
#include & lt; QJsonArray & gt;
#include & lt; QEventLoop & gt;
JSParse :: JSParse ()
{
}
JSParse :: ~ JSParse ()
{
}
void JSParse :: sendRequest () {
// create custom temporary event loop on stack
QEventLoop eventLoop;
// "quit ()" the event-loop, when the network request "finished ()"
QNetworkAccessManager mgr;
QObject :: connect (& amp; mgr, SIGNAL (finished (QNetworkReply *)), & amp; eventLoop, SLOT (quit ()));
// the HTTP request
QNetworkRequest req (QUrl (QString ("http://time.jsontest.com/")));
QNetworkReply * reply = mgr.get (req);
eventLoop.exec (); // blocks stack until "finished ()" has been called
if (reply- & gt; error () == QNetworkReply :: NoError) {
QString strReply = (QString) reply- & gt; readAll ();
// parse json
qDebug () & lt; & lt; "Response:" & lt; & lt; strReply;
QJsonDocument jsonResponse = QJsonDocument :: fromJson (strReply.toUtf8 ());
QJsonObject jsonObj = jsonResponse.object ();
qDebug () & lt; & lt; "Time:" & lt; & lt; jsonObj ["time"]. toString ();
qDebug () & lt; & lt; "Date:" & lt; & lt; jsonObj ["date"]. toString ();
delete reply;
}
else {
// failure
qDebug () & lt; & lt; "Failure" & lt; & lt; reply- & gt; errorString ();
delete reply;
}
}
The project file itself:
QT + = core gui opengl sql webkit webkitwidgets network
greaterThan (QT_MAJOR_VERSION, 4): QT + = widgets
TARGET = project01
TEMPLATE = app
SOURCES + = main.cpp \
mainwindow.cpp \
glscene.cpp \
sql.cpp \
jsparse.cpp
HEADERS + = mainwindow.h \
glscene.h \
sql.h \
jsparse.h
FORMS + = mainwindow.ui
What’s the problem?
Answer 1, authority 100%
This happens, although not often. I’ve seen it myself, for example, when you remove or add inheritance from QObject
for an arbitrary class after building the project.
On the other hand, this does not happen systemically, that is, not every time, so I cannot name the true reason.
Complete rebuilding of the project, including the stage with qmake does not help.
You must first delete all files in the project’s build folder, including the Makefile
. The build will then proceed without errors.