Unresolved external symbol
Unsolved
General and Desktop
-
Hello. Given the following code:
// portal.h #ifndef PORTAL_H #define PORTAL_H #include <QObject> #include <QPointer> class Portal : public QObject { Q_OBJECT public: explicit Portal(QObject *parent = nullptr); ~Portal(); static Portal* GetPortal(); bool connected; signals: void StateChanged(); private: static QPointer<Portal> self; }; #endif // PORTAL_H
// portal.cpp #include "Portal.h" Portal::Portal(QObject *parent) : QObject{parent} { self = this; } Portal::~Portal() { delete self; self = NULL; } Portal* Portal::GetPortal() { return self; }
I get the follwing error:
Portal.obj:-1: error: LNK2001: unresolved external symbol "private: static class QPointer<class Portal> Portal::self" (?self@Portal@@0V?$QPointer@VPortal@@@@A)
for the file portal.obj. I've tried everything I could think of and I can't find a way to fix it. -
@Mandar1jn
You have to define storage for classstatic
variable declarations:private: static QPointer<Portal> self;
In the
.cpp
you need somewhere outside of anything else, e.g. before the constructor:static QPointer<Portal> Portal::self;
Are you trying to turn C++ into Python? ;-)
-
Hi and welcome to devnet,
You just declared your static variable but you never initialized it.
That said, why such a singleton ?