Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在任何函数外部声明的变量大小类型_C++_Compiler Construction_Function_Declare - Fatal编程技术网

C++ 在任何函数外部声明的变量大小类型

C++ 在任何函数外部声明的变量大小类型,c++,compiler-construction,function,declare,C++,Compiler Construction,Function,Declare,在声明二维数组时 int random[height][width]; 然后在函数中使用它 void populate(int random[height][width], int x, int y) 给出在任何函数外部声明的错误变量大小类型。 我知道我做错了什么,这是件小事。我只是内存不好…您不能在函数外部定义具有非常量维度(宽度、高度)的数组,也就是说-不在堆栈帧中,因为在编译时维度是未知的。您必须使用常量或动态分配常量(在堆中或堆栈帧中)。您不能在函数之外定义具有非常量维度(宽度、高度

在声明二维数组时

int random[height][width];
然后在函数中使用它

void populate(int random[height][width], int x, int y)
给出在任何函数外部声明的错误变量大小类型。
我知道我做错了什么,这是件小事。我只是内存不好…

您不能在函数外部定义具有非常量维度(宽度、高度)的数组,也就是说-不在堆栈帧中,因为在编译时维度是未知的。您必须使用常量或动态分配常量(在堆中或堆栈帧中)。

您不能在函数之外定义具有非常量维度(宽度、高度)的数组,也就是说-不在堆栈帧中,因为在编译时维度是未知的。你可以使用常量或动态分配它(在堆中或栈帧中)。

< p>我现在要上紧,告诉你多维数组不值得在C或C++中进行大脑的努力。您最好使用一维数组(或者更好的是,使用标准容器)并编写索引函数:

inline int index (int x, int y)
{
  return x + y * width;
}
现在来解决你的问题。C++不支持C99可变长度数组。编译器必须在编译时知道数组的大小。例如,下面的方法不起作用

int dim = 4;
int ar[dim];
如果
dim
const
,它将起作用,因为编译器将能够准确地告诉
ar
应该有多宽(因为
dim
的值不会改变)。这可能就是你遇到的问题

如果希望能够在编译时更改大小,则需要执行更困难的操作,例如编写模板引用。由于多维数组在C/C++中的布局方式,因此不能将指针用于多维数组。模板化示例可能如下所示:

template <int Width, int Height>
void populate(int (&(&random)[Width])[Height], int x, int y);
模板
空隙填充(整数(&&random)[宽度][高度]、整数x、整数y);
这太难看了


运行时,你需要使用<代码>新< /Cord>来分配数据,或者使用容器类型。

< P>我现在要上紧,告诉你多维数组不值得在C或C++中进行大脑的努力。您最好使用一维数组(或者更好的是,使用标准容器)并编写索引函数:

inline int index (int x, int y)
{
  return x + y * width;
}
现在来解决你的问题。C++不支持C99可变长度数组。编译器必须在编译时知道数组的大小。例如,下面的方法不起作用

int dim = 4;
int ar[dim];
如果
dim
const
,它将起作用,因为编译器将能够准确地告诉
ar
应该有多宽(因为
dim
的值不会改变)。这可能就是你遇到的问题

如果希望能够在编译时更改大小,则需要执行更困难的操作,例如编写模板引用。由于多维数组在C/C++中的布局方式,因此不能将指针用于多维数组。模板化示例可能如下所示:

template <int Width, int Height>
void populate(int (&(&random)[Width])[Height], int x, int y);
模板
空隙填充(整数(&&random)[宽度][高度]、整数x、整数y);
这太难看了


对于运行时,您需要使用
new
来分配数据,或者使用容器类型。

您可以使用如下内容:

void populate(int height, int width, int **random)
{
    //here you can work from random[0][0] to random[height][width]
}
int main()
{
    int height=10;
    int width=20;
    int **myarray = new int*[height];
    for( int i=0; i< height; i++ ) myarray[i] = new int[width];
    populate( height, width, myarray);
}
然后你可以这样使用它:

void populate(int height, int width, int **random)
{
    //here you can work from random[0][0] to random[height][width]
}
int main()
{
    int height=10;
    int width=20;
    int **myarray = new int*[height];
    for( int i=0; i< height; i++ ) myarray[i] = new int[width];
    populate( height, width, myarray);
}
intmain()
{
整数高度=10;
整数宽度=20;
int**myarray=新int*[高度];
对于(int i=0;i

但是,当然,您必须注意缓冲区溢出。您可以使用以下方法:

void populate(int height, int width, int **random)
{
    //here you can work from random[0][0] to random[height][width]
}
int main()
{
    int height=10;
    int width=20;
    int **myarray = new int*[height];
    for( int i=0; i< height; i++ ) myarray[i] = new int[width];
    populate( height, width, myarray);
}
然后你可以这样使用它:

void populate(int height, int width, int **random)
{
    //here you can work from random[0][0] to random[height][width]
}
int main()
{
    int height=10;
    int width=20;
    int **myarray = new int*[height];
    for( int i=0; i< height; i++ ) myarray[i] = new int[width];
    populate( height, width, myarray);
}
intmain()
{
整数高度=10;
整数宽度=20;
int**myarray=新int*[高度];
对于(int i=0;i

但是,当然,当数组作为参数直接传递给函数(按值传递)时,您必须注意缓冲区溢出,它会衰减为指向数组第一个元素的指针。即使您可以在签名中清楚地读取数组的维度,编译器也会忽略这些维度。这种行为与C语言兼容

使用C++,可以通过引用传递数组,这将不再是问题。

int extract_value( int (&a)[10][10], int row, int col ) {
   return a[row][col];
}
int main() {
   int a[10][10] = {};
   a[5][5] = 1;
   std::cout << extract_value( a, 5, 5 ) << std::endl;
   int b[5][5];
//   extract_value( b, 2, 2 ); // error: the function takes an array of 10x10
}
int提取值(int(&a)[10][10],int行,int列){
返回一个[行][col];
}
int main(){
int a[10][10]={};
a[5][5]=1;

std::cout当数组作为参数直接传递给函数(按值传递)时,它会衰减为指向数组第一个元素的指针。即使您可以在签名中清楚地读取数组的维度,编译器也会忽略这些维度。这种行为与C兼容

使用C++,可以通过引用传递数组,这将不再是问题。

int extract_value( int (&a)[10][10], int row, int col ) {
   return a[row][col];
}
int main() {
   int a[10][10] = {};
   a[5][5] = 1;
   std::cout << extract_value( a, 5, 5 ) << std::endl;
   int b[5][5];
//   extract_value( b, 2, 2 ); // error: the function takes an array of 10x10
}
int提取值(int(&a)[10][10],int行,int列){
返回一个[行][col];
}
int main(){
int a[10][10]={};
a[5][5]=1;

我将用一个例子来说明:

//全球的

const int数组\u SIZE=16
结构ArrayType_t ArrayType[数组大小];

即使数组_SIZE被声明为常量int,它的值也不会在编译时初始化,因此编译器不知道数组的大小,并且给出了这样一个错误。但是如果将其作为哈希定义
#定义数组大小16
struct ArrayType\u t ArrayType[ARRAY\u SIZE]
==>之所以这样做,是因为ARRAY\u SIZE是在
编译时,编译器可以在编译时知道数组的大小。

我将用一个例子来说明:

//全球的

const int数组\u SIZE=16
结构ArrayType_t ArrayType[数组大小];

即使数组_SIZE被声明为常量int,它的值也不会在编译时初始化,因此编译器不知道数组的大小,并且给出了这样一个错误。Howev