Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unresolved external symbol
Forum Updated to NodeBB v4.3 + New Features

Unresolved external symbol

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 476 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • ? A Former User

      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.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Mandar1jn
      You have to define storage for class static 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? ;-)

      ? 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        You just declared your static variable but you never initialized it.

        That said, why such a singleton ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • JonBJ JonB

          @Mandar1jn
          You have to define storage for class static 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? ;-)

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @JonB thank you. That solved my issue

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved