C++编译器不允许我使用数组作为参数来调用用户定义的函数
C++ compiler not letting me use an array as parameter to function call in a userdefined function
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[]
尝试将参数列表声明为声明,其中不能作为函数中的参数传递。
您可能会因此而得到的确切错误是:
错误#[此处为错误号]:需要表达式
函数声明需要表达式,而不是声明。
相反,只需在函数声明中使用不带括号的gArray
。
将
数组传递给函数时不要包含括号。
替换此内容:
int average = GetAverage( gArray[], arrSize);
有了这个:
int average = GetAverage( gArray, arrSize);
相关文章:
- 为什么即使我调用参数化构造函数也会调用默认构造函数?
- 什么是仅调用一次并调用参数的控制台应用
- 调用参数排列不变函数 f(i++, i++)
- 调用参数不是原子参数的函数是此代码引发异常的原因吗?
- 如何使可调用参数的签名在C++模板中可见?
- x64 函数调用参数推送/移动顺序 (MSVC)
- 如何调用参数由另一个函数生成的函数?
- 从从可调用参数创建的线程对象参数移动构造 C++11 线程
- 正确调用参数化 std::函数
- 是否允许在调用参数中使用类型名 T
- 我不知道如何调用参数为节点类型的函数
- 函数调用参数列表 (C++) 中的空格
- Valgrind 错误:系统调用参数 epoll_pwait(sigmask) 指向不可寻址的字节
- 这C++代码如何让我调用参数数量错误的函数
- 在计算后,在基类之后调用参数化构造函数
- NPAPI调用参数包装
- 在类组合中调用参数化构造函数
- 为什么在创建类对象数组时不能调用参数化构造函数?
- 如何在 for 循环中调用参数化构造函数
- 如何在PIMPL设计模式中调用参数化构造函数