PDA

View Full Version : Structs for C++ and Qt



therealjag
6th March 2006, 18:50
just wondering if there is a set way of doing structs in qt. i have made structs before in the past using C, but im not too sure about using C++ to do it and to make it work with the Qt stuff. any idea's???

wysota
6th March 2006, 20:18
Try the same way as in C :)


struct Point {
int x;
int y;
};

BTW. Structs are treated by C++ as public classes, so you can add methods to them.

therealjag
7th March 2006, 17:34
ah ok thankyou...whilst im on the subject of storage, would it be easier to have a 2-d array of booleans or an array of linked lists??how would i create a 2-d array of booleans anyway because ive never tried it before - even with C??

GreyGeek
7th March 2006, 18:05
Try the same way as in C :)


struct Point {
int x;
int y;
};

BTW. Structs are treated by C++ as public classes, so you can add methods to them.

Ah... so that explains why Fullmetalcoder can, in the "devqthighlighter.h" header of his "DevQt" project, use


class CodeBlock;
....
....
struct CodeBlock{
...
}

It compiles with a ton of warnings, an executable, but no errors. If I change the declaration to "struct CodeBlock;" and compile then I get a ton of errors and no executable.

wysota
7th March 2006, 18:06
ah ok thankyou...whilst im on the subject of storage, would it be easier to have a 2-d array of booleans or an array of linked lists??how would i create a 2-d array of booleans anyway because ive never tried it before - even with C??

It depends. In general there are no such things as 2D arrays. How many bools do you need in each "row"? Maybe you could use bit fields?

therealjag
7th March 2006, 18:17
It depends. In general there are no such things as 2D arrays. How many bools do you need in each "row"? Maybe you could use bit fields?


well i'd probably need around 30 maximum but around 10 would do for now....what are bit fields though i have never used them before?

wysota
7th March 2006, 20:38
Then bitfields will do fine.


struct bfentry{
unsigned int
one:1,
two:1,
three:1,
four:1,
//...
thirtytwo:1;
}

The above structure occupies.... 4 bytes :) Making it an array is easy now;

Of course you can also use: std::vector< std::vector< bool > >

David812
6th June 2011, 11:46
Dear All,
I've a similar problem, i need a global struct such as:

struct _Stxy1000{
double dY[1000];
double dX[1000];
}
I've put the previous code in a .h file and the follow:

struct _Stxy1000 _stxy1000;
in a .cpp file which include the .h as definition.
When i use the struct field members in a .cpp, the compiler rise up some problems, such as "multiple definition" of the _stxy1000 variable.
Do you think i should use extern in the declaration?
Could you please post a c-like code??
Thanks in advance.
Andrea

wysota
6th June 2011, 12:56
I think you should definitely end your structure definition with a semicolon.