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. sharing static variable between functions
Forum Updated to NodeBB v4.3 + New Features

sharing static variable between functions

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.6k 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.
  • C Offline
    C Offline
    Chandras002
    wrote on 3 Aug 2018, 06:24 last edited by
    #1

    I'm trying to use a static variable so I can call it and use it 2 different functions on the same file.

    //
    #ifndef TESTNET_C_H
    #define TESTNET_C_H
    
    #include <QMainWindow>
    #include <QtNetwork>
    #include <stdlib.h>
    
    namespace Ui {
    class Testnet_c;
    }
    
    class Testnet_c : public QMainWindow
    {
        Q_OBJECT
    
    public:
      static QString strImageFilenameDir;
        explicit Testnet_c(QWidget *parent = 0);
        ~Testnet_c();
    
    private slots:
    
       clicked();
    
    private:
        Ui::Testnet_c *ui;
        QTcpSocket * tcpSocket;
      };
    
    #endif // TESTNET_C
    

    On the Testnet_c. cpp file, i am trying to use

    void Testnet_c::on_pushButton_3_clicked()    
    {
       QString exefileName;
    
     QString Testnet_c::strImageFilenameDir;  '
     // QString strImageFilenameDir;
    QString Testnet_c::strImageFilenameDir;
    strImageFilenameDir=txtfilenameinfo.absolutePath();
    
    }
    
    void Testnet_c::on_pushButton_4_clicked()    
    {
    
     strImageFilenameDir = "Some path name"
    }
    
    

    On Testnet_c button.cpp strImageFilenameDir get path on the pushButton_3 , it should be used on another function in the same file.
    Now, i'm getting error
    qualified-id in declaration before ';' token

    If did not declare the variable on header file, use it only in the .cpp file as static, getting the correct value for first function pushButton_3 and get " " for the pushButton _4 .
    i.e it does not retain value of the path (which it got on the earlier function)

    Am i missing something? Thanks a lot..

    Cheers!
    chandra

    J 1 Reply Last reply 3 Aug 2018, 06:32
    0
    • C Chandras002
      3 Aug 2018, 06:24

      I'm trying to use a static variable so I can call it and use it 2 different functions on the same file.

      //
      #ifndef TESTNET_C_H
      #define TESTNET_C_H
      
      #include <QMainWindow>
      #include <QtNetwork>
      #include <stdlib.h>
      
      namespace Ui {
      class Testnet_c;
      }
      
      class Testnet_c : public QMainWindow
      {
          Q_OBJECT
      
      public:
        static QString strImageFilenameDir;
          explicit Testnet_c(QWidget *parent = 0);
          ~Testnet_c();
      
      private slots:
      
         clicked();
      
      private:
          Ui::Testnet_c *ui;
          QTcpSocket * tcpSocket;
        };
      
      #endif // TESTNET_C
      

      On the Testnet_c. cpp file, i am trying to use

      void Testnet_c::on_pushButton_3_clicked()    
      {
         QString exefileName;
      
       QString Testnet_c::strImageFilenameDir;  '
       // QString strImageFilenameDir;
      QString Testnet_c::strImageFilenameDir;
      strImageFilenameDir=txtfilenameinfo.absolutePath();
      
      }
      
      void Testnet_c::on_pushButton_4_clicked()    
      {
      
       strImageFilenameDir = "Some path name"
      }
      
      

      On Testnet_c button.cpp strImageFilenameDir get path on the pushButton_3 , it should be used on another function in the same file.
      Now, i'm getting error
      qualified-id in declaration before ';' token

      If did not declare the variable on header file, use it only in the .cpp file as static, getting the correct value for first function pushButton_3 and get " " for the pushButton _4 .
      i.e it does not retain value of the path (which it got on the earlier function)

      Am i missing something? Thanks a lot..

      Cheers!
      chandra

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Aug 2018, 06:32 last edited by jsulm 8 Mar 2018, 06:33
      #2

      @Chandras002 said in sharing static variable between functions:

      QString Testnet_c::strImageFilenameDir;

      Move this to Testnet_c. cpp file. You do NOT define static variables inside functions!

      QString Testnet_c::strImageFilenameDir;
      
      void Testnet_c::on_pushButton_3_clicked()    
      {
      ...
      }
      

      See https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm

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

      1 Reply Last reply
      4
      • C Offline
        C Offline
        Chandras002
        wrote on 3 Aug 2018, 06:37 last edited by
        #3

        yes. it works. Thanks a lot. It made my Day :)

        similar to my problem here to discussed,. Just for someone's reference
        https://stackoverflow.com/questions/22566322/undefined-reference-to-statically-declared-variable-in-qt

        Cheers!
        chandra

        1 Reply Last reply
        0

        1/3

        3 Aug 2018, 06:24

        • Login

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