Qt5 Error LNK2019: unresolved external symbol
When adding the code
JSParse *jsp = new JSParse();
jsp->sendRequest();
In main.cpp
#include "mainwindow.h"
#include "sql.h"
#include <QApplication>
#include "jsparse.h"
int main(int argc, char *argv[])
{
Sql *sql = new Sql();
QApplication a(argc, argv);
MainWindow w;
w.show();
w.updateUsers(sql->users);
JSParse *jsp = new JSParse();
jsp->sendRequest();
return a.exec();
}
Returns errors:
main.obj:-1: ошибка: LNK2019: unresolved external symbol "public: __cdecl JSParse::JSParse(void)" (??0JSParse@@QEAA@XZ) referenced in function main
main.obj:-1: ошибка: LNK2019: unresolved external symbol "public: void __cdecl JSParse::sendRequest(void)" (?sendRequest@JSParse@@QEAAXXZ) referenced in function main
debug\project01.exe:-1: ошибка: LNK1120: 2 unresolved externals
Jsparse.h file
#ifndef JSPARSE_H
#define JSPARSE_H
class JSParse
{
public:
JSParse();
~JSParse();
void sendRequest();
};
#endif // JSPARSE_H
File jsparse.cpp
#include "jsparse.h"
#include <QDebug>
#include <QtWebKitWidgets/QWebFrame>
#include <QtWebKitWidgets/QWebPage>
#include <QtWebKitWidgets/QWebView>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QUrlQuery>
#include <QWebSettings>
#include <QVariant>
#include <QJsonValue>
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>
#include <QJsonArray>
#include <QEventLoop>
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(&mgr, SIGNAL(finished(QNetworkReply*)), &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->error() == QNetworkReply::NoError) {
QString strReply = (QString)reply->readAll();
//parse json
qDebug() << "Response:" << strReply;
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
qDebug() << "Time:" << jsonObj["time"].toString();
qDebug() << "Date:" << jsonObj["date"].toString();
delete reply;
}
else {
//failure
qDebug() << "Failure" <<reply->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 is the problem?
0
Author: Suvitruf - Andrei Apanasik, 2015-05-17
1 answers
This happens, although not often. I have observed when, for example, you remove or add inheritance from QObject
for an arbitrary class after building the project.
On the other hand, this does not happen systematically, that is, not every time, so I can not name the true reason.
Complete reassembly of the project, including stage c qmake it doesn't help.
You must first delete all the files in the project's build folder, including Makefile
. After that, the build it will pass without errors.
2
Author: alexis031182, 2017-11-21 10:13:41