简体   繁体   English

为什么我们在vs代码中写C++代码时不能通过一个变量来定义数组大小?

[英]Why can't we define the array size by a variable when we write C++ code in vs code?

Why can't we define the array size by a variable when writing C++ in vs code?为什么在vs代码中写C++时不能通过变量来定义数组大小?

l have typed the same code shown below in Dev C++, and such declaration is supported in Dev C++.我在 Dev C++ 中键入了如下所示的相同代码,并且在 Dev C++ 中支持这样的声明。

struct student_
{
    char name[11];
    char no[11];
    int grade;
};
struct student_ stu[n]; // not supported in vs code, but supported in Dev C++

I expect such declaration to be true, but the actual result is error.我希望这样的声明是正确的,但实际结果是错误的。

Variable length arrays are not and have never been a part of standard C++.可变长度数组不是也从未成为标准 C++ 的一部分。 Thus, the size of a statically allocated array must be known at compile time.因此,静态分配的数组的大小必须在编译时知道。 Some compilers support this functionality as a compiler extension (GCC does for instance), but it is not something portable.一些编译器支持此功能作为编译器扩展(例如 GCC),但它不是可移植的。 Use std::vector as a replacement.使用std::vector作为替代。

Arrays in C++ are static, and that's why their size needs to be known druing compile time so appropriate space can be allocated. C++ 中的数组是静态的,这就是为什么在编译时需要知道它们的大小,以便分配适当的空间。 You can initialize the size with a variable, but again that variable must have known constant value at compile time.您可以使用变量初始化大小,但该变量在编译时必须具有已知的常量值。 For that you can use constexpr keyword.为此,您可以使用constexpr关键字。

In order for your example to work, n must be declared like this:为了使您的示例正常工作,必须像这样声明n

constexpr size_t n = 5;

When you directly write a number into the size initializer, that value is naturally known at compile time and thus valid as well.当您直接将数字写入大小初始化程序时,该值在编译时自然是已知的,因此也是有效的。

To solve this issue, you need to select g++ compiler step 1: go to MinGw -> bin -> g++ -> press shift + right mouse button step 2: open visual studio -> go to view -> Command pallete -> search for C++ (UI) -> go to compiler path -> paste the g++ path and then scroll down and select IntelliSense mode select -> g++x64 All done!!要解决这个问题,您需要选择g++编译器步骤1:转到MinGw -> bin -> g++ -> 按shift +鼠标右键步骤2:打开Visual Studio -> 去查看-> 命令面板-> 搜索C++ (UI) -> 转到编译器路径 -> 粘贴 g++ 路径,然后向下滚动并选择 IntelliSense 模式选择 -> g++x64 全部完成! Happy Coding快乐编码

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

相关问题 为什么我们不能在c或c ++代码中使用直接寻址? - Why can't we use direct addressing in c or c++ code? 我们可以混合使用C和C ++代码吗 - Can we mix c and c++ code 如果我们没有在C ++中传递size变量,则通过函数查找数组的大小 - find the size of array through a function if we aren't passing size variable in c++ 为什么在C ++中输入数组时不能输入向量? - Why can't we input a vector as we input an array in C++? 我们可以在没有MSTest的VS2012或VS2010中获得本机C ++代码覆盖吗? - Can we get native C++ code coverage in VS2012 or VS2010 without MSTest? 当我们编译包含'main'而没有链接的源代码时,为什么我们不能运行它? - When we compile a source code that contains a 'main' without linking, why can't we run it? 为什么我们不编写可以处理 C++ 标识符的汇编器和链接器? - Why don't we write assemblers and linkers that can handle C++ identifiers? 我们如何在C ++中修改数组的大小? - How can we modify the size of array in C++? 为什么我们不能在 C++ 中转发声明具有已知大小的类/结构? - Why can't we forward declare classes/structs with known size in C++? 我们不能在 C++ 中声明一个字符串数据类型及其大小吗? - Can't we declare a string datatype in C++ with its size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM