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?

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

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 343 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 17 May 2024, 01:59 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...

    C 1 Reply Last reply 17 May 2024, 04:21
    0
    • M mzimmers
      17 May 2024, 01:59

      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...

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 May 2024, 04:21 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
      • M mzimmers has marked this topic as solved on 17 May 2024, 04:27
      • M Offline
        M Offline
        mzimmers
        wrote on 17 May 2024, 04:27 last edited by
        #3

        Aargh...forgot all about that. Thanks!

        J 1 Reply Last reply 17 May 2024, 18:22
        0
        • M mzimmers
          17 May 2024, 04:27

          Aargh...forgot all about that. Thanks!

          J Offline
          J Offline
          JoeCFD
          wrote on 17 May 2024, 18:22 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

          1/4

          17 May 2024, 01:59

          • Login

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