简体   繁体   中英

error C2131: expression did not evaluate to a constant while creating array of structure

i am trying to create array of structure. Method i was using for creating array of structure which was working fine in Linux and Mac but this is throwing error in windows

uint32_t size;
Test TestArray[size];
TestArray[i] = Test;
//i

Error I am getting in windows

error C2131: expression did not evaluate to a constant 

I have also tried

typedef struct Test {
    char *x;
    char *y;
} Test;

uint32_t size;
status = napi_get_array_length(env,args[2],&size);
assert(status == napi_ok);

struct Test  testList[size];
napi_value SharePrefixObject;
for(uint32_t i=0;i<size;i++){
Test t;
testList[i]= t;

Question How can resolve above error?

There are no variable length arrays in C++. The C++ way to do this is to use a vector.

Your code is very C like. The way you declare structs looks like C. The way you use pointers is idiomatically like C. Anyway, if you want to do some proper C++ programming then do this

#include <vector>

std::vector<Test> testList(size);

You need to use constant for array size like:

Test TestArray[123]; //were 123 - max size of your's array data

or

#define TEST_ARRAY_SIZE 123

Test TestArray[TEST_ARRAY_SIZE];

if you need different size use something like mallok:

uint32_t size;
Test *TestArrayPnt;

//some ware you got a size like size = 123

TestArrayPnt = new Test[size];

//continue a program. You can use TestArrayPnt [111] were 111 some offset less than size

delete[] TestArrayPnt; //when finish

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM