Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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++_Arrays_User Defined Functions - Fatal编程技术网

C++编译器不允许我使用数组作为参数来定义用户函数中的函数调用

C++编译器不允许我使用数组作为参数来定义用户函数中的函数调用,c++,arrays,user-defined-functions,C++,Arrays,User Defined Functions,C++编译器不允许我在用户定义函数中使用数组作为函数调用的参数。有人能给我解释一下并帮我解决吗 #include <iostream> using namespace std; double GetAverage(int[], int = 10); int GetAboveAverage(int[], int = 10); const int ARRAYSIZE = 10; void main() { int mArray[ARRAYSIZE]; cout &l

C++编译器不允许我在用户定义函数中使用数组作为函数调用的参数。有人能给我解释一下并帮我解决吗

#include <iostream>
using namespace std;
double GetAverage(int[], int = 10);
int GetAboveAverage(int[], int = 10);
const int ARRAYSIZE = 10;

void main()
{

    int mArray[ARRAYSIZE];

    cout << "Input the first number" << endl;

    for (int i = 0; i <= ARRAYSIZE - 1; i++)
    {
        cin >> mArray[i];
        cout << "Input the next number" << endl;
    }

    cout << "The average of the nummbers is " << GetAverage(mArray, ARRAYSIZE) << endl;
    cout <<"The the amount above average is " << GetAboveAverage(mArray, ARRAYSIZE) <<endl;

    system("pause");
}
出现问题的函数调用此函数

double GetAverage(int fArray[], int arrSize)
{

    int sum = 0;
    int average;

    for (int i = 0; i <= arrSize- 1; i++)
    sum += fArray[i];
    average = sum / arrSize;

    return average;
}
问题就在这里

int GetAboveAverage(int gArray[], int arrSize)
{
    int amtAboveAve;
    int average = GetAverage( gArray[], arrSize); //where i get the error its on the bracket and it says "error: expected and expression"
    for (int i = 0; i <= 9; i++)
        if (gArray[i] > average)
            amtAboveAve++;
    return amtAboveAve;

}
您无法按原样通过gArray[],因为它在您试图使用它的上下文中毫无意义。这是因为gArray[]试图声明参数列表,因为它是一个声明,不能在函数中作为参数传递

double GetAverage(int fArray[], int arrSize)
{

    int sum = 0;
    int average;

    for (int i = 0; i <= arrSize- 1; i++)
    sum += fArray[i];
    average = sum / arrSize;

    return average;
}
您可能因此而得到的确切错误是:

错误[此处的错误编号]:应为表达式

函数声明需要表达式,而不是声明


相反,只需在函数声明中使用不带括号的gArray。

将数组传递给函数时,不要包含括号

double GetAverage(int fArray[], int arrSize)
{

    int sum = 0;
    int average;

    for (int i = 0; i <= arrSize- 1; i++)
    sum += fArray[i];
    average = sum / arrSize;

    return average;
}
替换此项:

    int average = GetAverage( gArray[], arrSize); 
为此:

    int average = GetAverage( gArray, arrSize); 

你需要通过加里,而不是加里[]。gArray[]在此上下文中没有意义。谢谢,只是想知道为什么?在声明中只使用空方括号-例如,在参数列表中,int gArray[]是一个声明。传递给函数的参数是表达式,而不是声明。因此,这个错误需要一个表达式。哦,现在它是有意义的,我的编程老师决定加快这个单元。“以太好了,非常感谢。”罗萨里奥普莱拉我提供了一个答案。我刚刚意识到,这与这里的评论者所说的差不多,所以请点击投票按钮下方的绿色勾号,将我的答案标记为接受。