Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. how to declare global (static?) function inside class?
QtWS25 Last Chance

how to declare global (static?) function inside class?

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 341 Views
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I have a class with a member function that I'd like to use without having to instantiate an object of the class. I've never done this before, but this is what I tried:

    #pragma once
    
    #include <QObject>
    #include <QQmlEngine>
    
    class Clock : public QObject
    {
        Q_OBJECT
        QML_ELEMENT
    public:
        explicit Clock(QObject *parent = nullptr);
    
        static bool m_use24hourTime;
        static QString getTimeString(QTime time);
    }
    };
    

    I get a link error:

    undefined reference to `Clock::m_use24hourTime'
    

    so I'm obviously doing something wrong. I do use that variable in other routines in the class.

    Can someone tell me what I'm doing wrong here?

    Thanks...

    Christian EhrlicherC 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      I have a class with a member function that I'd like to use without having to instantiate an object of the class. I've never done this before, but this is what I tried:

      #pragma once
      
      #include <QObject>
      #include <QQmlEngine>
      
      class Clock : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
      public:
          explicit Clock(QObject *parent = nullptr);
      
          static bool m_use24hourTime;
          static QString getTimeString(QTime time);
      }
      };
      

      I get a link error:

      undefined reference to `Clock::m_use24hourTime'
      

      so I'm obviously doing something wrong. I do use that variable in other routines in the class.

      Can someone tell me what I'm doing wrong here?

      Thanks...

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mzimmers said in how to declare global (static?) function inside class?:

      so I'm obviously doing something wrong

      You just declare the variable but don't define them anywhere.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • mzimmersM mzimmers has marked this topic as solved on
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        Aargh...forgot all about that. Thanks!

        JoeCFDJ 1 Reply Last reply
        0
        • mzimmersM mzimmers

          Aargh...forgot all about that. Thanks!

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @mzimmers inline might be more convenient. No need to define it in cpp file.

          inline static bool m_use24hourTime{ false };
          
          1 Reply Last reply
          1

          • Login

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