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.
  • T Offline
    T Offline
    tomy
    wrote on 5 Sept 2017, 13:53 last edited by tomy 9 May 2017, 13:55
    #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
    • B Offline
      B Offline
      beecksche
      wrote on 5 Sept 2017, 14:02 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) { ... }
      
      T 2 Replies Last reply 5 Sept 2017, 14:09
      1
      • B beecksche
        5 Sept 2017, 14:02

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

        Add in your .cpp:

        int test1::i = 0;
        
        test1::test1(QWidget* parent)
            : QDialog(parent) { ... }
        
        T Offline
        T Offline
        tomy
        wrote on 5 Sept 2017, 14:09 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • B beecksche
          5 Sept 2017, 14:02

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

          Add in your .cpp:

          int test1::i = 0;
          
          test1::test1(QWidget* parent)
              : QDialog(parent) { ... }
          
          T Offline
          T Offline
          tomy
          wrote on 5 Sept 2017, 14:37 last edited by
          #4

          @beecksche
          Thank you. It was solved.

          1 Reply Last reply
          0

          3/4

          5 Sept 2017, 14:09

          • Login

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