简体   繁体   English

C ++默认数组大小使用命令行参数覆盖

[英]c++ default array size override with command line arguments

I saw one question on similar lines Specify Array from Command Line Argument 我在类似的行上看到一个问题,从命令行参数指定数组

Although my problem is bit different. 虽然我的问题有点不同。

I have multiple files implementing arrays of same size (NOC_SIZE). 我有多个文件实现相同大小的数组(NOC_SIZE)。 My program has a default mode and a user mode (command line arguments). 我的程序具有默认模式和用户模式(命令行参数)。 I gave the 'unsigned int NOC_SIZE = 16;' 我给了'unsigned int NOC_SIZE = 16;' line in my code before the start of main function. 在主功能开始之前在我的代码行中。 In another header file I declared a struct (noc_package) with parameter 'static unsigned int NOC_SIZE;'. 在另一个头文件中,我声明了一个带有参数“ static unsigned int NOC_SIZE;”的结构(noc_package)。 This header file in included in all files where ever required. 该头文件包含在所有需要的文件中。

But in the files where I have declared an array (int arr[noc_package :: NOC_SIZE]) it gives an error saying array bound not an integer. 但是在我声明了数组的文件中(int arr [noc_package :: NOC_SIZE]),它给出了一个错误,指出数组绑定的不是整数。

Can somebody suggest a way around this? 有人可以提出解决办法吗?

Thank you. 谢谢。

C++ doesn't support variable-length arrays. C ++不支持可变长度数组。

You're better of using an std::vector instead: 您最好使用std::vector代替:

std::vector<int> arr(noc_package::NOC_SIZE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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