Results 1 to 3 of 3

Thread: Using global struct

  1. #1
    Join Date
    Apr 2011
    Location
    Macerata, Italy
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using global struct

    Dear All,
    I've a problem using a global struct.
    Is possible to use it in Qt?
    I've tried to declare it in a global.h file a struct
    such as :
    Qt Code:
    1. extern struct _Stxy1000{
    2. double dY[];
    3. double dX[];
    4. }
    To copy to clipboard, switch view to plain text mode 
    then i've put in a global.cpp file
    Qt Code:
    1. struct _Stxy1000{
    2. double dY[1000];
    3. double dX[1000];
    4. }_stxy1000;
    To copy to clipboard, switch view to plain text mode 
    when i try i acces to the struct member the compiler rise up some errors such as, the struct is not declared, even if i include the global.h in a proper way.
    Thanks in advance

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Using global struct

    This is a very basic C++ thing and has nothing to do with Qt.

    Typically one would have in the header:

    Qt Code:
    1. typedef struct _Stxy1000_type {
    2. double dY[];
    3. double dX[];
    4. } _Stxy1000;
    5.  
    6. extern _Stxy1000 theVariable;
    To copy to clipboard, switch view to plain text mode 

    And in the .cpp file:
    Qt Code:
    1. _Stxy1000 theVariable;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using global struct

    Some corrections to mvuori's post. First, you don't need the typedef, that's C's stuff, it is not needed in C++. Second, you need to give size of those arrays, otherwise the compiler doesn't know how much memory the struct will take.
    Qt Code:
    1. struct _Stxy1000 {
    2. double dY[1000];
    3. double dX[1000];
    4. };
    5. extern _Stxy1000 theVariable;
    To copy to clipboard, switch view to plain text mode 

    to David812: Using global variables, although not wrong, is frowned upon. Remember, this is C++, not C
    You should instead wrap what ever functionality that is going to use that struct to inside a class where the variable is saved as a member variable and not as global.

Similar Threads

  1. QList<struct>
    By Axsis in forum Newbie
    Replies: 11
    Last Post: 12th October 2015, 07:48
  2. Struct into a C++ class
    By franco.amato in forum General Programming
    Replies: 24
    Last Post: 30th September 2010, 16:13
  3. A method that return a struct
    By PaceyIV in forum General Programming
    Replies: 5
    Last Post: 18th July 2009, 12:08
  4. Struct in network
    By sribalaji in forum Qt Programming
    Replies: 7
    Last Post: 26th March 2008, 10:38
  5. struct problem...
    By hiuao in forum General Programming
    Replies: 3
    Last Post: 5th April 2007, 07:48

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.