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?
QtWS25 Last Chance

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.
  • jsulmJ jsulm

    @thippu Please show your code (how you declare the static variable and how you're initialising it).
    It sounds like you did not define the static member. You need this in your cpp:

    double AmpConfiguration::sampling_rate;
    
    T Offline
    T Offline
    thippu
    wrote on last edited by thippu
    #3

    @jsulm okay.

    ampconfiguration.h

    static double sampling_rate;
    int getSampling_rate()const;
    void setSampling_rate(int value);
    
    static void ReadIni();
    
    

    ampconfiguration.cpp

    void AmpConfiguration::ReadIni()
    {
       QFile fp("AmpConfiguration.txt");
       if(!fp.open(QIODevice::ReadOnly)
      {
         QMessageBox::information::(nullptr,"file error","file not found",QMessageBox::Ok);
      }
    QStringList *line_list=new QStringList;
    QTextStream txt_stream(&fp);
    while(!txt_stream.atEnd())
    {
      QString line=txt_stream.readLine();
      QStringList temp=line.split("=",QString::SkipEmptyParts);
      line_list->push_back(temp.at(1));
     }
       sampling_rate=line_list->at(0).toDouble();
    }
    //I am getting error in the building itself not on the runtime.
    
    jsulmJ 1 Reply Last reply
    0
    • T thippu

      @jsulm okay.

      ampconfiguration.h

      static double sampling_rate;
      int getSampling_rate()const;
      void setSampling_rate(int value);
      
      static void ReadIni();
      
      

      ampconfiguration.cpp

      void AmpConfiguration::ReadIni()
      {
         QFile fp("AmpConfiguration.txt");
         if(!fp.open(QIODevice::ReadOnly)
        {
           QMessageBox::information::(nullptr,"file error","file not found",QMessageBox::Ok);
        }
      QStringList *line_list=new QStringList;
      QTextStream txt_stream(&fp);
      while(!txt_stream.atEnd())
      {
        QString line=txt_stream.readLine();
        QStringList temp=line.split("=",QString::SkipEmptyParts);
        line_list->push_back(temp.at(1));
       }
         sampling_rate=line_list->at(0).toDouble();
      }
      //I am getting error in the building itself not on the runtime.
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

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

      double AmpConfiguration::sampling_rate;
      

      to your cpp file?

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

      T 1 Reply Last reply
      2
      • jsulmJ jsulm

        @thippu Please show your code (how you declare the static variable and how you're initialising it).
        It sounds like you did not define the static member. You need this in your cpp:

        double AmpConfiguration::sampling_rate;
        
        T Offline
        T Offline
        thippu
        wrote on last edited by
        #5

        @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?

        jsulmJ 1 Reply Last reply
        0
        • T thippu

          @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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          • jsulmJ jsulm

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

            double AmpConfiguration::sampling_rate;
            

            to your cpp file?

            T Offline
            T Offline
            thippu
            wrote on 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

            jsulmJ 1 Reply Last reply
            0
            • T thippu

              @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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #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
              0
              • jsulmJ jsulm

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

                jsulmJ 1 Reply Last reply
                0
                • T thippu

                  @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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • jsulmJ jsulm

                    @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 last edited by thippu
                    #11

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

                    jsulmJ 1 Reply Last reply
                    0
                    • T thippu

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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
                      1
                      • jsulmJ jsulm

                        @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 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
                        • jsulmJ jsulm

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

                          jsulmJ 1 Reply Last reply
                          0
                          • T thippu

                            @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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #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
                            3
                            • jsulmJ jsulm

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

                              • Login

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