c++指针数组中的错误

时间:2022-09-30 21:17:32

I have a piece of code:

我有一段代码:

int CPUs = GetNumCPUs();
FILE *newFile[CPUs];

I got an error. It marks 'CPUs' on the second line and says: "expression must have a constant value".

我有一个错误。它在第二行标记“cpu”,并说:“表达式必须具有一个常量值”。

I have tried to use const but it is not working.

我试着使用const,但是没有用。

6 个解决方案

#1


5  

You can't have a varible sized array in C++. Adding const to CPUs doesn't help, it only makes the variable read-only, but it's still not a compile time constant because it's initialized by a function at run-time.

在c++中不能有一个可变大小的数组。向cpu添加const没有帮助,它只使变量成为只读的,但它仍然不是编译时常量,因为它在运行时由函数初始化。

The usual solution is to use a vector:

通常的解决办法是使用向量:

std::vector<FILE*> newFile(CPUs);

#2


3  

The value of GetNumCPUs() may or may not change every time you run the program - so it is not constant. If you want an array that has a variable amount of elements, try std::vector:

GetNumCPUs()的值可能会在每次运行程序时改变,也可能不会——所以它不是常量。如果您想要一个元素数量可变的数组,请尝试std::vector:

std::vector<FILE*> newFile(GetNumCPUs());

#3


2  

In your code, const doesn't mean "constant". In this context, it means the object is read-only — i.e you can't modify the object. You're trying to create a variable length array, which isn't allowed in C++. Use std::vector, use new to allocate memory, or write a C99 program where VLAs like the one you're trying to make are allowed.

在您的代码中,const并不意味着“常量”。在这个上下文中,它意味着对象是只读的- i。你不能修改对象。您正在尝试创建一个可变长度数组,这在c++中是不允许的。使用std::vector,使用new来分配内存,或者编写一个C99程序,允许您尝试创建的VLAs。

#4


0  

Using a const doesn't fix all of your problems in this scenario. The problem is that arrays must be initialized at compile time, so if you have a function return a variable (GetNumCPUs()) and assign it to a constant (const int CPUs), the variable isn't known at compile time but runtime, and the compiler can't allocate data space for the array.

在这个场景中,使用const并不能解决所有问题。问题是数组必须在编译时进行初始化,因此,如果有一个函数返回一个变量(GetNumCPUs())并将其分配给一个常量(const int CPUs),那么这个变量在编译时而运行时是未知的,并且编译器不能为这个数组分配数据空间。

Using an std::vector, however, allows for variable storage space.

然而,使用std::vector允许使用可变存储空间。

std::vector<FILE*> newFile(CPUs);

This should work fine. Here's a couple tutorials:

这应该工作很好。这里有几个教程:

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/c -教程- -初学者指南- - stdvector - 1. htm一部分

http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/

http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/

#5


-2  

CPUs is no known at compile time.

在编译时不知道cpu。

#6


-2  

You should dynamically create the array with new[]

您应该使用new[]动态创建数组

int CPUs = GetNumCPUs();
FILE** newFile = new (FILE*)[CPUs];

After you're done with it, you're now responsible for deleting it as well:

完成之后,你现在也要负责删除它:

delete[] newFile;

#1


5  

You can't have a varible sized array in C++. Adding const to CPUs doesn't help, it only makes the variable read-only, but it's still not a compile time constant because it's initialized by a function at run-time.

在c++中不能有一个可变大小的数组。向cpu添加const没有帮助,它只使变量成为只读的,但它仍然不是编译时常量,因为它在运行时由函数初始化。

The usual solution is to use a vector:

通常的解决办法是使用向量:

std::vector<FILE*> newFile(CPUs);

#2


3  

The value of GetNumCPUs() may or may not change every time you run the program - so it is not constant. If you want an array that has a variable amount of elements, try std::vector:

GetNumCPUs()的值可能会在每次运行程序时改变,也可能不会——所以它不是常量。如果您想要一个元素数量可变的数组,请尝试std::vector:

std::vector<FILE*> newFile(GetNumCPUs());

#3


2  

In your code, const doesn't mean "constant". In this context, it means the object is read-only — i.e you can't modify the object. You're trying to create a variable length array, which isn't allowed in C++. Use std::vector, use new to allocate memory, or write a C99 program where VLAs like the one you're trying to make are allowed.

在您的代码中,const并不意味着“常量”。在这个上下文中,它意味着对象是只读的- i。你不能修改对象。您正在尝试创建一个可变长度数组,这在c++中是不允许的。使用std::vector,使用new来分配内存,或者编写一个C99程序,允许您尝试创建的VLAs。

#4


0  

Using a const doesn't fix all of your problems in this scenario. The problem is that arrays must be initialized at compile time, so if you have a function return a variable (GetNumCPUs()) and assign it to a constant (const int CPUs), the variable isn't known at compile time but runtime, and the compiler can't allocate data space for the array.

在这个场景中,使用const并不能解决所有问题。问题是数组必须在编译时进行初始化,因此,如果有一个函数返回一个变量(GetNumCPUs())并将其分配给一个常量(const int CPUs),那么这个变量在编译时而运行时是未知的,并且编译器不能为这个数组分配数据空间。

Using an std::vector, however, allows for variable storage space.

然而,使用std::vector允许使用可变存储空间。

std::vector<FILE*> newFile(CPUs);

This should work fine. Here's a couple tutorials:

这应该工作很好。这里有几个教程:

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/c -教程- -初学者指南- - stdvector - 1. htm一部分

http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/

http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/

#5


-2  

CPUs is no known at compile time.

在编译时不知道cpu。

#6


-2  

You should dynamically create the array with new[]

您应该使用new[]动态创建数组

int CPUs = GetNumCPUs();
FILE** newFile = new (FILE*)[CPUs];

After you're done with it, you're now responsible for deleting it as well:

完成之后,你现在也要负责删除它:

delete[] newFile;