Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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+,则无法将2D数组传递到函数中+;_C++_Arrays - Fatal编程技术网

C++ 如果没有错误消息C+,则无法将2D数组传递到函数中+;

C++ 如果没有错误消息C+,则无法将2D数组传递到函数中+;,c++,arrays,C++,Arrays,我无法将名为“sales”的2D数组传递给函数。知道我做错了什么吗?我有C++作为在线课程,我的老师没有帮助:(我得到的错误信息是: 重载函数“getTotal”的实例与参数不匹配 列表以及“COLS”:未声明的标识符 getTotal函数 不接受2个参数“ //第7周作业2 #包括 #包括 使用名称空间std; //功能原型 int getTotal(int[][COLS]); //全局变量 const int ROWS=4; 常数int COLS=4; 常数int NUM_DIVS=5; 常

我无法将名为“sales”的2D数组传递给函数。知道我做错了什么吗?我有C++作为在线课程,我的老师没有帮助:(我得到的错误信息是:

重载函数“getTotal”的实例与参数不匹配 列表以及“COLS”:未声明的标识符

getTotal函数 不接受2个参数“

//第7周作业2
#包括
#包括
使用名称空间std;
//功能原型
int getTotal(int[][COLS]);
//全局变量
const int ROWS=4;
常数int COLS=4;
常数int NUM_DIVS=5;
常数int NUM_QTRS=5;
国际销售额[行][COLS];
国际总销售额;
字符串划分[NUM_DIVS]={“北”、“南”、“东”、“西”、“四分之一总计”};
字符串quarters[NUM_QTRS]={“第1季度”、“第2季度”、“第3季度”、“第4季度”};
整数合计;
int main()
{
//变数

cout首先,在定义COLS之前,您正在使用它,但您不能这样做,因此,您可以通过在尝试使用它之前先定义它来修复它:

// Global Variables
const int ROWS = 4;
const int COLS = 4;//this is defined here

int getTotal(int[][COLS]);//now the compiler knows what COLS is
其次,正如编译器正确指出的那样,您的
getTotal
函数只接受一个参数,但在
total=getTotal(sales,4)中传递了两个参数
。我相信您正在尝试将行大小作为第二个参数传递,但在您的情况下没有必要这样做,因为您的所有函数都可以将
定义视为全局变量(事实上,您使用的是
内部
getTotal
函数)

您还应该注意,由于数组被声明为全局的,因此无需向
getTotal
函数传递任何参数,尽管我不建议您在代码中如此广泛地使用全局变量

 int getTotal();//function prototype without parameters since sales array is global variable
 total = getTotal(); //call the function

您可能还需要重新查看
getTotal
中代码的逻辑,因为当前的实现,如将同一变量
count
定义为两个循环计数器,将导致意外的结果。

您必须在使用变量之前声明变量。您首先使用
COLS
,然后声明它。除了err或者你提到你的代码中有另一个错误。你的函数只返回第一行的总和。非常感谢bkVnet的可能重复项…我现在已经启动并运行了该函数,多亏了你。虽然此任务还需要5个函数,但我希望能应用相同的技术使其工作。我真的很感激。
total = getTotal(sales); //this is enough to call the function
 int getTotal();//function prototype without parameters since sales array is global variable
 total = getTotal(); //call the function