我正在尝试创建结构数组。我用来创建结构数组的方法,该方法在Linux和Mac中运行良好,但在windows中抛出错误。
uint32_t size;
Test TestArray[size];
TestArray[i] = Test;
//i进入windows时出错
error C2131: expression did not evaluate to a constant 我也试过
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;问题:如何解决上述错误?
发布于 2020-04-08 14:24:50
C++中没有可变长度数组。实现这一点的C++方法是使用向量。
你的代码很像C语言。声明结构的方式类似于C,使用指针的方式类似于习惯用法的C。不管怎样,如果您想进行一些适当的C++编程,那么可以这样做。
#include <vector>
std::vector<Test> testList(size);发布于 2020-04-08 14:07:31
您需要使用常量作为数组大小,如下所示:
Test TestArray[123]; //were 123 - max size of your's array data或
#define TEST_ARRAY_SIZE 123
Test TestArray[TEST_ARRAY_SIZE];如果您需要不同大小,请使用类似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 finishhttps://stackoverflow.com/questions/61094030
复制相似问题