c++ - 在我的代码中,表达式必须有一个指向对象类型的指针是什么意思?
问题描述
这是出现最多错误的函数
double getHighestRainfall(double arrayOfNumbers, double SIZE)
{
double highest;
highest = arrayOfNumbers[0];
for(int count = 0; count > highest ; count++)
{
if(arrayOfNumbers[count] > highest)
{
highest = arrayOfNumbers[count];
}
}
return highest;
}
#include <iostream>
using namespace std;
//function prototypes
double getLowestValue(double arrayOfNumbers, double SIZE);
double takeAverage(double arrayOfNumbers, double average);
double getLowestValue(double arrayOfNumbers, double SIZE);
double getHighestRainfall(double arrayOfNumbers, double SIZE);
double getTotalRainfall(double arrayOfNumbers[], double SIZE);
//global variables and constants
double average;
const double SIZE = 12;
double arrayOfNumbers[12];
double TOTAL = 0;
string myMonths[];
//main function
int main ()
{
//declare the string for months
string myMonths[12] = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"};
double arrayOfNumbers[12];
//get values to store in array
for(int count = 0; count < SIZE; count++)
{
cout << "Enter values to store in " << myMonths[count] << ": ";
cin >> arrayOfNumbers[count];
}
//print the total rainfall
cout << "The total rainfall is: " << getTotalRainfall(arrayOfNumbers,
SIZE);
//print the average rainfall
cout << "The average rainfall is: " << takeAverage(average, SIZE);
//print the least rainfall
cout << "The least rainfall is: " << getLowestValue(arrayOfNumbers, SIZE);
return 0;
}
错误:
test2.cpp:66:39: error: no matching function for call to 'getLowestValue'
cout << "The least rainfall is: " << getLowestValue(arrayOfNumbers, SIZE);
BenedictionsMBP:MyCppFiles benedictionbora$ g++ test2.cpp
test2.cpp:16:8: error: definition of variable with array type needs an explicit size or an initializer
string myMonths[];
^
test2.cpp:47:39: error: no matching function for call to 'getLowestValue'
cout << "The least rainfall is: " << getLowestValue(arrayOfNumbers, SIZE);
^~~~~~~~~~~~~~
test2.cpp:6:8: note: candidate function not viable: no known conversion from 'double [12]' to 'double' for 1st argument
double getLowestValue(double arrayOfNumbers, double SIZE);
^
test2.cpp:102:29: error: subscripted value is not an array, pointer, or vector
highest = arrayOfNumbers[0];
~~~~~~~~~~~~~~^~
test2.cpp:105:22: error: subscripted value is not an array, pointer, or vector
if(arrayOfNumbers[count] > highest)
~~~~~~~~~~~~~~^~~~~~
test2.cpp:107:29: error: subscripted value is not an array, pointer, or vector
highest = arrayOfNumbers[count];
~~~~~~~~~~~~~~^~~~~~
解决方案
Adouble
是单个值,您不能索引双精度值。如果您想要一个数组(因为问题被标记为 C++),请使用std::vector或std::array:
double getHighestRainfall(const std::vector<double> &arrayOfNumbers)
arrayOfNumbers.size()
然后保存向量中元素的数量,因此您不需要将此数字作为参数传递。然后循环将是for(int count = 0; count < numberOfArrays.size(); count++)
如果您被允许使用,请std::vector
使用它并跳过其余的答案。如果由于某种原因不允许使用,则std::vector
需要使用指针:
double getHighestRainfall(const double *arrayOfNumbers, size_t size)
可能SIZE
是数组中元素的数量?然后你使用size_t
as 类型,因为double
是浮点数,这里没有意义。然后循环需要for(int count = 0; count < size; count++)
推荐阅读
- excel - 从 Word VBA 返回值到调用 Excel VBA
- javascript - 空的初始化 javascript 对象包含许多属性和函数
- r - 如何在 R 中将一些数据显示为表格?
- javascript - 有没有办法从回调中访问这个变量?
- node.js - 使用 makeAugmentedSchema 时必须提供 typeDefs
- python - 为什么我的斐波那契函数版本不起作用?
- python - 熊猫在每个组中获得最重要的不同记录
- mysql - 在 MySQL 表的 Default/Expression 中使用 case 表达式
- typescript - 如何在构造函数中设置未知的打字稿成员变量
- apache-spark - 为什么使用 sql 表达式进行过滤比在 Spark 中的 DataSet 中应用的函数更好