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. How to define static members in a static function in .cpp file?
Forum Update on Monday, May 27th 2025

How to define static members in a static function in .cpp file?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 2 Posters 3.0k 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.
  • T thippu
    4 Feb 2019, 08:20

    @jsulm said in How to define static members in a static function in .cpp file?:

    double AmpConfiguration::sampling_rate;

    double AmpConfiguration::sampling_rate=10.2 //like this? in the static function?

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 4 Feb 2019, 08:24 last edited by
    #6

    @thippu said in How to define static members in a static function in .cpp file?:

    in the static function?

    No. Like this:

    double AmpConfiguration::sampling_rate=10.2;
    void AmpConfiguration::ReadIni()
    

    See https://en.cppreference.com/w/cpp/language/static

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • J jsulm
      4 Feb 2019, 08:12

      @thippu Not really what I asked...
      Did you add

      double AmpConfiguration::sampling_rate;
      

      to your cpp file?

      T Offline
      T Offline
      thippu
      wrote on 4 Feb 2019, 08:24 last edited by
      #7

      @jsulm yes bro, I did add like this double AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
      but new error came qualified-id decalration before "=" token

      J 1 Reply Last reply 4 Feb 2019, 08:25
      0
      • T thippu
        4 Feb 2019, 08:24

        @jsulm yes bro, I did add like this double AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
        but new error came qualified-id decalration before "=" token

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 4 Feb 2019, 08:25 last edited by jsulm 2 Apr 2019, 08:27
        #8

        @thippu said in How to define static members in a static function in .cpp file?:

        double AmpConfiguration::sampling_rate=line_list->at(0).toDouble();

        Please read again what I wrote.
        Where do you call this now?
        It looks like you're now trying to declare a LOCAL variable with same name which is for sure not what you want to do.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        T 1 Reply Last reply 4 Feb 2019, 08:26
        0
        • J jsulm
          4 Feb 2019, 08:25

          @thippu said in How to define static members in a static function in .cpp file?:

          double AmpConfiguration::sampling_rate=line_list->at(0).toDouble();

          Please read again what I wrote.
          Where do you call this now?
          It looks like you're now trying to declare a LOCAL variable with same name which is for sure not what you want to do.

          T Offline
          T Offline
          thippu
          wrote on 4 Feb 2019, 08:26 last edited by
          #9

          @jsulm said in How to define static members in a static function in .cpp file?:

          Where do you call this now?

          In the mainwindow.cpp

          J 1 Reply Last reply 4 Feb 2019, 08:27
          0
          • T thippu
            4 Feb 2019, 08:26

            @jsulm said in How to define static members in a static function in .cpp file?:

            Where do you call this now?

            In the mainwindow.cpp

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 4 Feb 2019, 08:27 last edited by
            #10

            @thippu said in How to define static members in a static function in .cpp file?:

            In the mainwindow.cpp

            WHERE EXACTLY?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply 4 Feb 2019, 08:30
            0
            • J jsulm
              4 Feb 2019, 08:27

              @thippu said in How to define static members in a static function in .cpp file?:

              In the mainwindow.cpp

              WHERE EXACTLY?

              T Offline
              T Offline
              thippu
              wrote on 4 Feb 2019, 08:30 last edited by thippu 2 Apr 2019, 08:31
              #11

              @jsulm
              In the constructor of mainwindow.cpp
              I'm calling AmpConfiguration::ReadIni(); First statement itself

              J 1 Reply Last reply 4 Feb 2019, 08:33
              0
              • T thippu
                4 Feb 2019, 08:30

                @jsulm
                In the constructor of mainwindow.cpp
                I'm calling AmpConfiguration::ReadIni(); First statement itself

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 4 Feb 2019, 08:33 last edited by
                #12

                @thippu Can you actually be more precise when answering questions?
                Do you mean you put that line in the constructor or inside ReadIni()?
                But as I said: you're declaring a local variable with same name.
                It must be like this:

                AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
                

                AmpConfiguration:: can be removed as well...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 2 Replies Last reply 4 Feb 2019, 08:41
                1
                • J jsulm
                  4 Feb 2019, 08:33

                  @thippu Can you actually be more precise when answering questions?
                  Do you mean you put that line in the constructor or inside ReadIni()?
                  But as I said: you're declaring a local variable with same name.
                  It must be like this:

                  AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
                  

                  AmpConfiguration:: can be removed as well...

                  T Offline
                  T Offline
                  thippu
                  wrote on 4 Feb 2019, 08:41 last edited by
                  #13

                  @jsulm said in How to define static members in a static function in .cpp file?:

                  local variable

                  I would like it to be a global static variable.

                  and I would like to use it(initialize) inside AmpConfiguration::ReadIni()

                  @jsulm said in How to define static members in a static function in .cpp file?:

                  AmpConfiguration::sampling_rate=line_list->at(0).toDouble();

                  finally, Calling the AmpConfiguration::ReadIni()in constructor of mainwindow.cpp.

                  1 Reply Last reply
                  0
                  • J jsulm
                    4 Feb 2019, 08:33

                    @thippu Can you actually be more precise when answering questions?
                    Do you mean you put that line in the constructor or inside ReadIni()?
                    But as I said: you're declaring a local variable with same name.
                    It must be like this:

                    AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
                    

                    AmpConfiguration:: can be removed as well...

                    T Offline
                    T Offline
                    thippu
                    wrote on 4 Feb 2019, 08:44 last edited by
                    #14

                    @jsulm said in How to define static members in a static function in .cpp file?:

                    AmpConfiguration::sampling_rate=line_list->at(0).toDouble();

                    No, change in the error. still, I'm getting undefined reference

                    J 1 Reply Last reply 4 Feb 2019, 08:54
                    0
                    • T thippu
                      4 Feb 2019, 08:44

                      @jsulm said in How to define static members in a static function in .cpp file?:

                      AmpConfiguration::sampling_rate=line_list->at(0).toDouble();

                      No, change in the error. still, I'm getting undefined reference

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 4 Feb 2019, 08:54 last edited by jsulm 2 Apr 2019, 08:59
                      #15

                      @thippu "I would like it to be a global static variable." - then why do you declare it as local variable?
                      Did you put

                      double AmpConfiguration::sampling_rate=10.2; // This has to be OUTSIDE of any method/constructor
                      void AmpConfiguration::ReadIni()
                      {
                          sampling_rate=line_list->at(0).toDouble();
                      }
                      

                      as I already wrote above?
                      Please read about static members in C++.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      T 1 Reply Last reply 4 Feb 2019, 09:32
                      3
                      • J jsulm
                        4 Feb 2019, 08:54

                        @thippu "I would like it to be a global static variable." - then why do you declare it as local variable?
                        Did you put

                        double AmpConfiguration::sampling_rate=10.2; // This has to be OUTSIDE of any method/constructor
                        void AmpConfiguration::ReadIni()
                        {
                            sampling_rate=line_list->at(0).toDouble();
                        }
                        

                        as I already wrote above?
                        Please read about static members in C++.

                        T Offline
                        T Offline
                        thippu
                        wrote on 4 Feb 2019, 09:32 last edited by
                        #16

                        @jsulm said in How to define static members in a static function in .cpp file?:

                        // This has to be OUTSIDE of any method/constructor

                        Worked. bro
                        Thank you

                        1 Reply Last reply
                        0

                        15/16

                        4 Feb 2019, 08:54

                        • Login

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