Results 1 to 4 of 4

Thread: Static variable problem

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Static variable problem

    Hi,

    I am having problem while using static variables. I want to retain screen dimensions as static variable values and use it across many classes. Hence I have following case.

    Utilities class is common class having many general methods.

    utilities.h
    ===========
    Qt Code:
    1. #ifndef UTILITIES_H
    2. #define UTILITIES_H
    3.  
    4. #include <QtWidgets>
    5. class Utilities{
    6. static int ScreenWidth;
    7. public:
    8. explicit Utilities();
    9.  
    10. int screenDimension(){
    11. return ScreenWidth;
    12. }
    13. void setScreenDimension(int pr){
    14. ScreenWidth = pr;
    15. }
    16. };
    17. #endif // UTILITIES_H
    To copy to clipboard, switch view to plain text mode 


    MainWindow class is a class which will have main window and will assign value to ScreenWidth variable of Utilities class.

    mainwindow.h
    ============
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtWidgets>
    5. #include "utilities.h"
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. protected:
    19. void resizeEvent(QResizeEvent *e);
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. Utilities ut;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    ==============
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::resizeEvent(QResizeEvent *e)
    17. {
    18. QSize graphicsViewSize = ui->graphicsView->size();
    19. ut.setScreenDimension(graphicsViewSize.width);
    20. }
    To copy to clipboard, switch view to plain text mode 

    When I compile this it gives me an error as below

    H:\Sample\mainwindow.cpp:-1: error: undefined reference to `Utilities::ScreenWidth'



    Thanks

    Manish

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Static variable problem

    Looks like you forgot to define the static field.

    One compilation unit (.cpp file) of your application needs to "hold the memory" location for the static variable.

    Qt Code:
    1. int Utilities::ScreenWidth = 0;
    To copy to clipboard, switch view to plain text mode 

    Btw, are you sure you want a static member variable and non-static accessors?

    Cheers,
    _

  3. #3
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Static variable problem

    Thanks. Yes it has solved the problem. By the way what is the risk in this approach? Why i should not use static variables accessed by non static accessors.




    Manish

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Static variable problem

    No risk, but why do you want to create an instance of a class that does not contain instance data?

    Cheers,
    _

Similar Threads

  1. Side effect to const static variable - do not know how
    By Prototyp144 in forum General Programming
    Replies: 0
    Last Post: 4th June 2013, 15:25
  2. Replies: 4
    Last Post: 4th August 2011, 13:47
  3. Replies: 0
    Last Post: 4th August 2011, 12:37
  4. Replies: 22
    Last Post: 8th October 2008, 14:54
  5. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 11:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.