I'm trying to implement a simple server client application in QT. I'm using Visual Studio 2017.
Here's the Header File to my client class.
#pragma once
#ifndef Client_H
#define Client_H
#include <QTcpSocket>
class Client : public QObject
{ Q_OBJECT
public:
explicit Client (QObject *parent = 0);
void Connect();
signals:
public slots:
private:
QTcpSocket *socket;
};
#endif
and here's the code to Client.cpp
#include "Client.h"
Client::Client(QObject *parent) :
QObject(parent)
{
}
void Client::Connect()
{
socket = new QTcpSocket(this);
socket->connectToHost("192.168.10.10", 8016);
if (socket->waitForConnected(3000))
{
printf("COnnected");
}
else
{
printf("Not Connected");
}
}
as you can see it's pretty simple. In my Main.cpp i create an instance of the client class and then call the function Connect():
Client Test;
Test.Connect();
When building my project i get the following errors
1>Client.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Client::metaObject(void)const " (?metaObject#Client##UEBAPEBUQMetaObject##XZ)
1>Client.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl Client::qt_metacast(char const *)" (?qt_metacast#Client##UEAAPEAXPEBD#Z)
1>Client.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl Client::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#Client##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
I know that this Error means that the functions are declared but the definition is missing. I just don't know where to find these definitions and which file i have to link to get rid of these errors.
Thank you
These files are automatically generated by the moc tool. Did you set it up to run whenever your header file changed? Did you run it manually? If not, consider installing the Qt Visual Studio tools that solve this for you.
Related
I tried to override the mouseMoveEvent method by subclassing ChartView and I get a linking error which has something to do with the constructor of ChartView class.
class ChartView : public QChartView
{
Q_OBJECT
public:
ChartView(QChart* chart, QWidget* parent = 0);
protected:
void mouseMoveEvent(QMouseEvent* event) override;
};
ChartView::ChartView(QChart* chart, QWidget* parent)
: QChartView(chart, parent)
{
this->setMouseTracking(true);
}
void ChartView::mouseMoveEvent(QMouseEvent* event)
{
qDebug() << event->pos();
}
The error:
1>mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl ChartView::metaObject(void)const " (?metaObject#ChartView##UEBAPEBUQMetaObject##XZ)
1>mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl ChartView::qt_metacast(char const *)" (?qt_metacast#ChartView##UEAAPEAXPEBD#Z)
1>mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl ChartView::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#ChartView##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
When I remove the constructor of ChartView, the problem is gone, but I dont know why, because I also dont understand the error.
What am I doing wrong and how can I fix this problem?
Before building your application, you need to clean qmake and run it again. Then, you can rebuild the solution. Problem is fixed.
I am trying to run a simple piece of code using the documentation provided here https://doc.qt.io/qt-5/qnetworkaccessmanager.html. I am using Qt Creator with MSVC2017 64-bit Kit on Windows 10. My knowledge of C++ and Classes is very basic.
The code gives linker errors I don't know why. I have tried cleaning and re-building again but no success. Any help would be appreciated. Thanks!
I create a new project in Qt Creator with mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void replyFinished(QNetworkReply *reply);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
And then add these lines in mainwindow.cpp and compile.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished,
this, &MainWindow::replyFinished);
manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
//
}
I get linker errors at the output when I compile using MSVC2017 64-bit Kit. The issue is same if I use a different Kit (MinGW).
Here's my .pro file :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# 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 += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Here's the compiler output:
12:52:23: Running steps for project Proj2...
12:52:23: Configuration unchanged, skipping qmake step.
12:52:23: Starting: "C:\Qt\Tools\QtCreator\bin\jom.exe"
C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
link /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST:embed /OUT:debug\Proj2.exe #C:\Users\HP\AppData\Local\Temp\Proj2.exe.7376.15.jom
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QNetworkRequest::QNetworkRequest(class QUrl const &)" (__imp_??0QNetworkRequest##QEAA#AEBVQUrl###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QNetworkRequest::~QNetworkRequest(void)" (__imp_??1QNetworkRequest##QEAA#XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QNetworkAccessManager::QNetworkAccessManager(class QObject *)" (__imp_??0QNetworkAccessManager##QEAA#PEAVQObject###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QNetworkAccessManager::~QNetworkAccessManager(void)" (__imp_??1QNetworkAccessManager##UEAA#XZ) referenced in function "public: virtual void * __cdecl QNetworkAccessManager::`scalar deleting destructor'(unsigned int)" (??_GQNetworkAccessManager##UEAAPEAXI#Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QNetworkReply * __cdecl QNetworkAccessManager::get(class QNetworkRequest const &)" (__imp_?get#QNetworkAccessManager##QEAAPEAVQNetworkReply##AEBVQNetworkRequest###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QNetworkAccessManager::finished(class QNetworkReply *)" (__imp_?finished#QNetworkAccessManager##QEAAXPEAVQNetworkReply###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
mainwindow.obj : error LNK2001: unresolved external symbol "protected: virtual class QNetworkReply * __cdecl QNetworkAccessManager::createRequest(enum QNetworkAccessManager::Operation,class QNetworkRequest const &,class QIODevice *)" (?createRequest#QNetworkAccessManager##MEAAPEAVQNetworkReply##W4Operation#1#AEBVQNetworkRequest##PEAVQIODevice###Z)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl QNetworkAccessManager::metaObject(void)const " (?metaObject#QNetworkAccessManager##UEBAPEBUQMetaObject##XZ)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl QNetworkAccessManager::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#QNetworkAccessManager##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl QNetworkAccessManager::qt_metacast(char const *)" (?qt_metacast#QNetworkAccessManager##UEAAPEAXPEBD#Z)
mainwindow.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const QNetworkAccessManager::staticMetaObject" (__imp_?staticMetaObject#QNetworkAccessManager##2UQMetaObject##B)
Hint on symbols that are defined and could potentially match:
"__declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject" (__imp_?staticMetaObject#QMainWindow##2UQMetaObject##B)
mainwindow.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const QNetworkReply::staticMetaObject" (__imp_?staticMetaObject#QNetworkReply##2UQMetaObject##B)
Hint on symbols that are defined and could potentially match:
"__declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject" (__imp_?staticMetaObject#QMainWindow##2UQMetaObject##B)
debug\Proj2.exe : fatal error LNK1120: 12 unresolved externals
Your .pro file lacks network module import. Change first line to add network library.
QT += core gui network
You can see this from the documentation, it says there
qmake: QT += network
Im working on a project using Qt/C++ originally compiled with MinGW64 (gcc 4.8). I decided to "port" it to MSVC2013, since I need to use some Windows API functions that doesn't work well in MinGW, but right now I'm stuck at this error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl IMLoginView::clearFieldPass(void)" (?clearFieldPass#IMLoginView##QEAAXXZ) referenced in function "public: void __cdecl MainWindow::showConnectionErrorDialog(void)" (?showConnectionErrorDialog#MainWindow##QEAAXXZ)
imingestinglist.obj:-1: error: LNK2019: unresolved external symbol "public: static class QString __cdecl IMStorageSystem::getLogDir(void)" (?getLogDir#IMStorageSystem##SA?AVQString##XZ) referenced in function "private: class QList<int> __cdecl IMIngestingList::loadList(void)" (?loadList#IMIngestingList##AEAA?AV?$QList#H##XZ)
moc_imloginview.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl IMLoginView::login(void)" (?login#IMLoginView##QEAAXXZ) referenced in function "private: static void __cdecl IMLoginView::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall#IMLoginView##CAXPEAVQObject##W4Call#QMetaObject##HPEAPEAX#Z)
moc_imloginview.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl IMLoginView::handleError(enum ALFRED_ACCESS_ERROR)" (?handleError#IMLoginView##QEAAXW4ALFRED_ACCESS_ERROR###Z) referenced in function "private: static void __cdecl IMLoginView::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall#IMLoginView##CAXPEAVQObject##W4Call#QMetaObject##HPEAPEAX#Z)
moc_imloginview.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl IMLoginView::userDisconnected(void)" (?userDisconnected#IMLoginView##QEAAXXZ) referenced in function "private: static void __cdecl IMLoginView::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)"
The problem is: These methods aren't part of some external library, but part of the project itself. And I already triple checked that they are implemented. They aren't part of any template class either, but normal classes, and the log says the cpp files are being compiled. The same project links just fine with MinGW64.
I'm currently using Qt Creator 3.3.2 and QT 5.3.2.
What can be happening?
EDIT:
I Can't show much code, since its a closed-source project, but I can at least show the header and implementation layout of the methods:
imloginview.h:
class IMLoginView : public QWidget
{
Q_OBJECT
public:
explicit IMLoginView(QWidget *parent = 0);
void clearFieldPass();
~IMLoginView();
private:
//Lots of attributes
void configureComponents();
void keyPressEvent(QKeyEvent *event);
signals:
public slots:
void login();
void handleError(ALFRED_ACCESS_ERROR error);
void userDisconnected();
}
imloginview.cpp:
IMLoginView::IMLoginView(QWidget *parent) : //lots of initializations here
{
//Implementation
}
IMLoginView::~IMLoginView()
{
}
void IMLoginView::clearFieldPass()
{
//implementation
}
void IMLoginView::configureComponents()
{
//Implementation
}
void IMLoginView::keyPressEvent(QKeyEvent *event)
{
//implementation
}
void IMLoginView::login()
{
//implementation
}
void IMLoginView::handleError(ALFRED_ACCESS_ERROR error)
{
//implementation
}
void IMLoginView::userDisconnected()
{
//implementation
}
I'm sorry I really can't provide the actual method code, but its closed-source. =/
I'm new to C++ and have encountered a problem while running my app. I googled the problem but since most results were with linking libraries I started a new thread.
I have a class CResizableDialog which I'm inheriting from my VtkDialogTest2 dialog class.
VtkDialogTest2.h;
#pragma once
#include "CResizableDialog.h"
#ifdef _WIN32_WCE
#error "CDHtmlDialog is not supported for Windows CE."
#endif
// VtkDialogTest2 dialog
class VtkDialogTest2 : public CResizableDialog
{
DECLARE_DYNCREATE(VtkDialogTest2)
public:
VtkDialogTest2(CWnd* pParent = NULL); // standard constructor
virtual ~VtkDialogTest2();
// Overrides
HRESULT OnButtonOK(IHTMLElement *pElement);
HRESULT OnButtonCancel(IHTMLElement *pElement);
// Dialog Data
enum { IDD = IDD_DIALOG4 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
VtkDialogTest2.cpp
#include "stdafx.h"
#include "Geometry.h"
#include "VtkDialogTest2.h"
IMPLEMENT_DYNCREATE(VtkDialogTest2, CResizableDialog)
VtkDialogTest2::VtkDialogTest2(CWnd* pParent /*=NULL*/)
: CResizableDialog(VtkDialogTest2::IDD, pParent),
{
}
VtkDialogTest2::~VtkDialogTest2()
{
}
void VtkDialogTest2::DoDataExchange(CDataExchange* pDX)
{
CResizableDialog::DoDataExchange(pDX);
}
BOOL VtkDialogTest2::OnInitDialog()
{
CResizableDialog::OnInitDialog();
//some code
return TRUE; // return TRUE unless you set the focus to a control
}
BEGIN_MESSAGE_MAP(VtkDialogTest2, CResizableDialog)
ON_BN_CLICKED(IDOK, &VtkDialogTest2::OnBnClickedOk)
END_MESSAGE_MAP()
//some code
I can't figure out what I'm doing wrong. I downloaded an example from the web which uses the CResizableDialog.h class the exact same way and copied both CResizableDialog.h and CResizableDialog.cpp into my project.
The errors I'm getting are;
1>VtkDialogTest2.obj : error LNK2019: unresolved external symbol "public: __thiscall CResizableDialog::CResizableDialog(unsigned int,class CWnd *)" (??0CResizableDialog##QAE#IPAVCWnd###Z) referenced in function "public: __thiscall VtkDialogTest2::VtkDialogTest2(class CWnd *)" (??0VtkDialogTest2##QAE#PAVCWnd###Z)
1>VtkDialogTest2.obj : error LNK2019: unresolved external symbol "protected: virtual int __thiscall CResizableDialog::OnInitDialog(void)" (?OnInitDialog#CResizableDialog##MAEHXZ) referenced in function "protected: virtual int __thiscall VtkDialogTest2::OnInitDialog(void)" (?OnInitDialog#VtkDialogTest2##MAEHXZ)
1>VtkDialogTest2.obj : error LNK2001: unresolved external symbol "protected: static struct AFX_MSGMAP const * __stdcall CResizableDialog::GetThisMessageMap(void)" (?GetThisMessageMap#CResizableDialog##KGPBUAFX_MSGMAP##XZ)
1>C:\Users\Geometry.exe : fatal error LNK1120: 3 unresolved externals
Any input will be highly appreciated.
The error was because I copied the CResizableDialog.h and CResizableDialog.cpp files directly into the project folder. I later noticed that they didn't show up in the solution window and copied them to the window as well. After that the errors disappeared.
I'm creating a QLabel subclass which adds the DoubleClickEvent to it. I have created the following, but I'm getting some strange linker errors, maybe someone can point out what I've done wrong?
//Header
#ifndef IMAGE_LABEL_H
#define IMAGE_LABEL_H
#include <QLabel>
#include <QMouseEvent>
class image_label : public QLabel
{
Q_OBJECT
public:
image_label(QWidget* parent = 0);
~image_label();
signals:
void doubleClicked();
protected:
void mouseDoubleClickEvent(QMouseEvent * e);
};
#endif
//CPP
#include "image_label.h"
#include <QMouseEvent>
image_label::image_label(QWidget* parent) : QLabel(parent)
{
}
image_label::~image_label()
{
}
void image_label::mouseDoubleClickEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
{
emit doubleClicked();
QLabel::mouseDoubleClickEvent(e);
}
}
I get the following linker errors when I compile:
image_label.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall image_label::metaObject(void)const " (?metaObject#image_label##UBEPBUQMetaObject##XZ)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall image_label::qt_metacast(char const *)" (?qt_metacast#image_label##UAEPAXPBD#Z)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall image_label::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#image_label##UAEHW4Call#QMetaObject##HPAPAX#Z)
image_label.obj : error LNK2019: unresolved external symbol "protected: void __thiscall image_label::doubleClicked(void)" (?doubleClicked#image_label##IAEXXZ) referenced in function "protected: virtual void __thiscall image_label::mouseDoubleClickEvent(class QMouseEvent *)" (?mouseDoubleClickEvent#image_label##MAEXPAVQMouseEvent###Z)
Can anybody help why I get these errors?
You must run the MOC preprocessor on the file image_label.h. This generates a file moc_image_label.cppthat you must include in the build. The error message indicates that you have not done this. (The symbols image_label::metaObject etc. that are mentioned in the error message are defined in moc_image_label.cpp.)