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. Using static variables in programs
Forum Updated to NodeBB v4.3 + New Features

Using static variables in programs

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.5k Views 2 Watching
  • 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    Hi,

    Please take a look at this shortened version of the real app. I built this program just to follow and solve the issue in a simpler way. How to use a static variable. well, of course, my purpose is to prevent various objects to have a copy of that.

    #ifndef TEST1_H
    #define TEST1_H
    #include <QDialog>
    
    class QLineEdit;
    class QPushButton;
    
    class test1 : public QDialog
    {
        Q_OBJECT
    
    public:
        test1(QWidget* parent = 0);
    
    private slots:
        void show_clicked();
    
    private:
        static int i;
        QLineEdit* line;
        QPushButton* show_number;
        QPushButton* quit;
    
    };
    
    #endif // TEST1_H
    
    #include <QtWidgets>
    #include "test1.h"
    
    test1::test1(QWidget* parent)
        : QDialog(parent)
    {
      i = 5;
     line = new QLineEdit;
      show_number = new QPushButton(tr("show"));
      quit = new QPushButton(tr("Quit"));
    
      connect(show_number, SIGNAL(clicked(bool)),
              this, SLOT(show_clicked()));
      connect(quit, SIGNAL(clicked(bool)), this, SLOT(close));
    
      QVBoxLayout* layout = new QVBoxLayout();
      layout->addWidget(line);
      layout->addWidget(show_number);
      layout->addWidget(quit);
      setLayout(layout);
    }
    
    //**********************************
    
    void test1::show_clicked()
    {
      line->setText(QString::number(i));
    }
    
    #include<QApplication>
    #include "test1.h"
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        test1* t1 = new test1;
        t1->show();
        return app.exec();
    }
    

    When I run the code I get 3 errors:

    error: undefined reference to `test1::i'

    error: undefined reference to `test1::i'

    error: error: ld returned 1 exit status

    1 Reply Last reply
    0
    • beeckscheB Offline
      beeckscheB Offline
      beecksche
      wrote on last edited by
      #2

      Hi @tomy ,
      you have to define them "outside" again:

      Add in your .cpp:

      int test1::i = 0;
      
      test1::test1(QWidget* parent)
          : QDialog(parent) { ... }
      
      tomyT 2 Replies Last reply
      1
      • beeckscheB beecksche

        Hi @tomy ,
        you have to define them "outside" again:

        Add in your .cpp:

        int test1::i = 0;
        
        test1::test1(QWidget* parent)
            : QDialog(parent) { ... }
        
        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • beeckscheB beecksche

          Hi @tomy ,
          you have to define them "outside" again:

          Add in your .cpp:

          int test1::i = 0;
          
          test1::test1(QWidget* parent)
              : QDialog(parent) { ... }
          
          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #4

          @beecksche
          Thank you. It was solved.

          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