Results 1 to 3 of 3

Thread: Declare global array of a struct in another header file

  1. #1
    Join Date
    Jul 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Declare global array of a struct in another header file

    Hi ,

    I have a structure that defines something for example personal information , I have this struct in my Globals.h file , something like this :

    Qt Code:
    1. struct Persons{
    2. QString Name;
    3. QString Email;
    4. int Age;
    5. QString Gender;
    6. };
    To copy to clipboard, switch view to plain text mode 

    in Globals.cpp , I defined an array of this struct like this :

    Qt Code:
    1. extern Persons myPers[100];
    To copy to clipboard, switch view to plain text mode 

    now , I have to use this array everywhere of my application , in different Threads , but I can not , I am new in c++ and QT , but I have basic knowledge of c++ programming , is my array definition correct ? is my structure definition correct , what is the safe and right way to use this array ?

    Best Regards,
    Amir .

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Declare global array of a struct in another header file

    This is a general C++ question; it is not specific to Qt.

    Your definition of struct Persons look correct.

    The line
    Qt Code:
    1. extern Persons myPers[100];
    To copy to clipboard, switch view to plain text mode 
    is not a definition, but a declaration. Along with the definition of struct Persons, it should appear in each source file (i.e. .cpp file) that needs to access the myPers array. It tells the compiler everything it needs to know about the array in order to use it, without actually allocating and initializing it. Instead of copying this line in every source file, people usually put it in a .h that they then #include in the .cpp file. You did just that with struct Persons by defining it in Globals.h; you could simply move the declaration of the array there.

    Now, after the compiler has compiled all your .cpp files, it will try to link them together to form an autonomous executable. The linker will complain because no one has allocated the myPers array. You should therefore define the array somewhere in a .cpp file (and only there). The definition looks just like the declaration, but without extern:
    Qt Code:
    1. Persons myPers[100];
    To copy to clipboard, switch view to plain text mode 
    (Note: this definition default-initializes the array; there are other ways to initialize the array, but this is beyond the scope of this post.)

    Although all of this is correct, this is C-style programming (structures, plain arrays and global variables), as opposed to C++-style (classes and containers).

    Finally, you mentioned threads in your post. Different threads can access different objects concurrently, but they must synchronize whenever one of them writes to an object when another thread might be reading from the same object. You can prevent several threads from accessing the same object at a given time by protecting all accesses with a mutex (see QMutex, for instance). Depending on the amount of concurrency you want to allow, you could have a global mutex for the whole array, or a mutex for each element, etc.

  3. The following user says thank you to yeye_olive for this useful post:

    persianpatient (14th September 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Declare global array of a struct in another header file

    Dear yeye_olive,

    Thank you very much for your explanation .
    now I'm trying to use QMutex in my application .

    Best Regards,
    Amir .

Similar Threads

  1. Use 1 global header file for all declarations?
    By grayfox in forum General Programming
    Replies: 1
    Last Post: 22nd July 2011, 16:47
  2. Using global struct
    By David812 in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2011, 13:45
  3. How to declare global variable outside a class?
    By babygal in forum Qt Programming
    Replies: 2
    Last Post: 26th August 2010, 08:35
  4. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  5. how to declare a global variable right
    By Cruz in forum Newbie
    Replies: 13
    Last Post: 16th February 2010, 16:25

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.