I'm working in a C++ project where I'm trying to create a struct which posses an array, whose size will be determined when the method is called. This, though, is giving me the famous error "array bound is not an integer constant" (using GCC and Qt).
I did some research in StackOverflow and other places but couldn't find any solution for my particular situation: either the size of the array was clearly non-constant, or else the problem didn't appear JUST when the array is located inside a struct, not outside of it.
To give the code I used for testing:
void QuantitySelectorCenterView::accepted()
{
const int numItems = modelSelectedList->rowCount();
uchar selectedItemsX[numItems];
struct PQDataRequest
{
re8k_ict_header header;
re8k_ict_physical_quantity quantity;
uchar selectedItems[numItems];
};
struct PQDataRequest2
{
re8k_ict_header header;
re8k_ict_physical_quantity quantity;
uchar selectedItems[10];
};
}
In the following code, the "rowCount()" value of the modelSelectedList is defined at runtime, depending on the configuration the user sets. When he press the OK button, "accepted" is called. In a first moment, the compiler disliked the rowCount() returned value since it was a common int; I put its value in a const int, "numItems". Don't know if this actually changes something, but the declaration of an array (selectedItemsX) didn't returned any error. I'ld expect, therefore, that I could use such code. But when I created the struct "PQDataRequest", the compiler gave that error for the "selectedItems" array inside it. The SAME array, now having problems for non-const size! And the second struct shown in the code above don't present any errors.
So why can I declare and use an array such as selectedItemsX outside a struct declaration, but I can't use an exactly equal array inside a struct? And how may I overcome this problem? Notice that I can't use a variable-size container such as vector because the same algorithm needs to be implemented later in a C code in a similar fashion, and the usage of a pointer to an array inside the struct is problematic since I'll need to use sizeof() in the struct later, and I can only know the size of the array at runtime when accepted() is called.
Thanks for any help,
Momergil