PDA

View Full Version : Using global struct



David812
6th June 2011, 12:12
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 :

extern struct _Stxy1000{
double dY[];
double dX[];
} then i've put in a global.cpp file

struct _Stxy1000{
double dY[1000];
double dX[1000];
}_stxy1000;
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

mvuori
6th June 2011, 12:20
This is a very basic C++ thing and has nothing to do with Qt.

Typically one would have in the header:



typedef struct _Stxy1000_type {
double dY[];
double dX[];
} _Stxy1000;

extern _Stxy1000 theVariable;


And in the .cpp file:


_Stxy1000 theVariable;

joyer83
6th June 2011, 14:45
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.


struct _Stxy1000 {
double dY[1000];
double dX[1000];
};
extern _Stxy1000 theVariable;


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.