数组 "expression must have a constant"
Array "expression must have a constant"
我需要将 C# 代码转换为 C++,虽然不是一次都没有移动 C++,只是结合了不同部分的内容和外观,然后以某种方式工作。我现在主要有一个数组的问题,给我一个错误作为主题。
public:
int arrimg( Bitmap^ image)
{
const int w = image->Width;
const int h = image->Height;
const int z = 3;
int dtab[w][h][z];
for (int x = 0; x < image->Width; x++)
{
for (int y = 0; y < image->Height; y++)
{
dtab[x][ y][ 0] = image->GetPixel(x, y).R;
dtab[x][ y][ 1] = image->GetPixel(x, y).G;
dtab[x][ y][ 2] = image->GetPixel(x, y).B;
}
}
return dtab;
}
错误仍然出现在我身上 "in" 和 "h" 并且不知道如何修复它。第二个问题,如果我为 "a" 和 "h" 给出严格的数字也是 "dtab" 崩溃错误,“Rerturn type does not match the function type.
使用 std::vector
而不是可变长度数组 (VLA)。
例如:
std::vector< std::vector< std::vector<int> > > dtab;
或者动态分配数组:
int * dtab = new int [w * h * z];
编译器会向您发出明确的错误消息:您不能拥有可变大小的局部数组。您必须使用 new 显式分配 dtab 或使用类似 std::vector 的东西。此外,return 类型是错误的,因为您的代码试图 return int 的 3D 数组,但 return 类型被声明为单个 int。
C++(显然也不是 C++/CLI(您正在使用的))不支持 variable-length arrays。
虽然您的代码中的 w
和 h
可能是常量,但它们只是常量,意思是它们不能被修改。它们不是 compile-time 常量,这是数组维度所必需的。
另一个上的变量z
是一个compile-time常量,因为它的值可以由编译器设置,不依赖于其他run-time变量或值。
如果你想坚持使用纯 C++ 解决方案,那就是使用 std::vector
。
您使用的是哪个编译器?
下面这行代码
int dtab[w][h][z];
在 Visual Studio 中不起作用,但 gcc
和 clang
对此很慷慨。例如,代码
int i;
cin>>i;
int arr[i]
生成与您在问题中遇到的错误相同的错误,但可以与其他编译器一起编译并完美运行
原因是 visual studio 期望 C++ 数组是 constant
,如果它们不是常量,您需要使用数组创建内存。
您需要更改两行以使用 multi-dimensional cli::array
cli::array<int, 3>^ arrimg(System::Drawing::Bitmap^ image)
...
auto dtab = gcnew cli::array<int, 3>(w, h, z);
注意 C# 中的 cli::array<int, 3>^
语法而不是 int[,,]
;并使用 gcnew
而不是 new
。为了完整起见,完整的代码是:
cli::array<int, 3>^ arrimg(System::Drawing::Bitmap^ image)
{
const int w = image->Width;
const int h = image->Height;
const int z = 3;
auto dtab = gcnew cli::array<int, 3>(w, h, z);
for (int x = 0; x<image->Width; x++)
{
for (int y = 0; y<image->Height; y++)
{
dtab[x, y, 0] = image->GetPixel(x, y).R;
dtab[x, y, 1] = image->GetPixel(x, y).G;
dtab[x, y, 2] = image->GetPixel(x, y).B;
}
}
return dtab;
}
我需要将 C# 代码转换为 C++,虽然不是一次都没有移动 C++,只是结合了不同部分的内容和外观,然后以某种方式工作。我现在主要有一个数组的问题,给我一个错误作为主题。
public:
int arrimg( Bitmap^ image)
{
const int w = image->Width;
const int h = image->Height;
const int z = 3;
int dtab[w][h][z];
for (int x = 0; x < image->Width; x++)
{
for (int y = 0; y < image->Height; y++)
{
dtab[x][ y][ 0] = image->GetPixel(x, y).R;
dtab[x][ y][ 1] = image->GetPixel(x, y).G;
dtab[x][ y][ 2] = image->GetPixel(x, y).B;
}
}
return dtab;
}
错误仍然出现在我身上 "in" 和 "h" 并且不知道如何修复它。第二个问题,如果我为 "a" 和 "h" 给出严格的数字也是 "dtab" 崩溃错误,“Rerturn type does not match the function type.
使用 std::vector
而不是可变长度数组 (VLA)。
例如:
std::vector< std::vector< std::vector<int> > > dtab;
或者动态分配数组:
int * dtab = new int [w * h * z];
编译器会向您发出明确的错误消息:您不能拥有可变大小的局部数组。您必须使用 new 显式分配 dtab 或使用类似 std::vector 的东西。此外,return 类型是错误的,因为您的代码试图 return int 的 3D 数组,但 return 类型被声明为单个 int。
C++(显然也不是 C++/CLI(您正在使用的))不支持 variable-length arrays。
虽然您的代码中的 w
和 h
可能是常量,但它们只是常量,意思是它们不能被修改。它们不是 compile-time 常量,这是数组维度所必需的。
另一个上的变量z
是一个compile-time常量,因为它的值可以由编译器设置,不依赖于其他run-time变量或值。
如果你想坚持使用纯 C++ 解决方案,那就是使用 std::vector
。
您使用的是哪个编译器?
下面这行代码
int dtab[w][h][z];
在 Visual Studio 中不起作用,但 gcc
和 clang
对此很慷慨。例如,代码
int i;
cin>>i;
int arr[i]
生成与您在问题中遇到的错误相同的错误,但可以与其他编译器一起编译并完美运行
原因是 visual studio 期望 C++ 数组是 constant
,如果它们不是常量,您需要使用数组创建内存。
您需要更改两行以使用 multi-dimensional cli::array
cli::array<int, 3>^ arrimg(System::Drawing::Bitmap^ image)
...
auto dtab = gcnew cli::array<int, 3>(w, h, z);
注意 C# 中的 cli::array<int, 3>^
语法而不是 int[,,]
;并使用 gcnew
而不是 new
。为了完整起见,完整的代码是:
cli::array<int, 3>^ arrimg(System::Drawing::Bitmap^ image)
{
const int w = image->Width;
const int h = image->Height;
const int z = 3;
auto dtab = gcnew cli::array<int, 3>(w, h, z);
for (int x = 0; x<image->Width; x++)
{
for (int y = 0; y<image->Height; y++)
{
dtab[x, y, 0] = image->GetPixel(x, y).R;
dtab[x, y, 1] = image->GetPixel(x, y).G;
dtab[x, y, 2] = image->GetPixel(x, y).B;
}
}
return dtab;
}