variably modified type in char[]
I have this struct. What I am trying to do is to have a continues ram space to memcpy them on hard drive. I have a dynamic created string which I will use as 开发者_运维问答a key. I want to create a struct that can do this. I used templates and I made this.
template <class ItemType> struct INXM_Node {
ItemType key;
int left;
int right;
int next; // Used for queue.
} ;
I was running:
INXM_Node<char[100]> *root = new INXM_Node<char[100]>();
Everything was fine until I tried to change 100 with a variable. Then i got error:
'char [(((long unsigned int)(((long int)attrLength) - 1)) + 1u)]' is a variably modified type
What I ran was:
sizeof(INXM_Node<char[attrLength]>);
I am taking attrLength as an argument from a function. I need to generate multiple structs with different char arrays.
The problem is that the compiler needs to know what type ItemType
is at compile time. When you use a variable, it cannot know. The compiler attempts to specifically create each ItemType
that will be used in the execution of your program. If you are using a variable length char
array, the compiler does not know how much memory to allocate for that particular ItemType
. You might consider using std::string
The type you use to instantiate a template must be fixed at compile time. When you compile with a template, specific code is emitted by the compiler, for the different types you use with the template. This can't be done at run time (there might not be a compiler available even) and it would be unreasonable and indeed impossible to expect it to be done for every possible type at compile time.
I think you're taking the wrong approach to your problem in general though. It would be better to use std::string
as the key if you need the size to vary at run time and use something like boost::serialize to (portably and safely) save your data to disk.
精彩评论