How to define a QBitArray variable in a struct?
-
Is u the same as vec ?
Do you set the size of the bit array somewhere? If not then accessing bit 3 of array of size 0 will obviously be undefined behavior (crash in your case). -
With that syntax you're trying to call a function(a constructor). You can't call a function at a declaration. It's a compile-time construct.
You have several choices:
@
//if your compiler supports c++11 use uniform initialization:
struct mm{
QBitArray bb{8};
int val;
};//if it doesn't you can either define a constructor:
struct mm{
mm() : bb(8) {}
QBitArray bb;
int val;
};//or just resize the thing later on:
struct mm{
QBitArray bb;
int val;
};
//somewhere later:
mm foo;
foo.bb.resize(8);
@ -
new mm, not new foo. Then you would resize each element in a for loop:
@
mm* foo = new mm[10];
for(int i=0; i< 10; ++i)
foo[i].bb.resize(8);
@
But I would favor the first option. the loop is then unnecessary.