C++ 在我的代码中,表达式必须有指向对象类型的指针意味着什么?
这是出现最多错误的函数C++ 在我的代码中,表达式必须有指向对象类型的指针意味着什么?,c++,C++,这是出现最多错误的函数 double getHighestRainfall(double arrayOfNumbers, double SIZE) { double highest; highest = arrayOfNumbers[0]; for(int count = 0; count > highest ; count++) { if(arrayOfNumbers[count] > highest) {
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];
~~~~~~~~~~~~~~^~~~~~
问题在于arrayOfNumbers不是指针而是双精度索引,它没有意义
此外,for循环条件是错误的。您需要迭代数组的所有元素
你可能想写的是:
double getHighestRainfall(double * arrayOfNumbers, std::size_t SIZE)
{
if(SIZE > 0)
{
double highest = arrayOfNumbers[0];
for(std::size_t count = 0; count < SIZE ; ++count)
{
if(arrayOfNumbers[count] > highest)
highest = arrayOfNumbers[count];
}
return highest;
}
else
{
return 0; // Default value if the array is empty. You can also raise an exception instead.
}
}
为了避免显式访问未保留的内存位置,我添加了对数组大小的检查
注意:您可能必须使用std::array而不是原始数组。如果您需要一个动态数组,那么std::vector将适合您的需要。您会遇到这个错误,因为您必须传递一个指向数组的指针。这是正确的
double getHighestRainfall(double * arrayOfNumbers, int SIZE)
{
if(!SIZE) return 0; // default 0 if SIZE is zero
double highest = arrayOfNumbers[0];
for(int count = 0; count < SIZE; ++count){
if(arrayOfNumbers[count] > highest)
highest = arrayOfNumbers[count];
}
return highest;
}
int main(){
double testcase[3] = {100.1 , 102.3 , 99.8};
cout << "Highest Rain Fall: " << getHighestRainfall(testcase,3);
return 0;
}
一些提示:
大小应该是int,因为数组长度是整数,实际上它应该是无符号int
出于某些原因,请在循环中使用++计数。它是否具有性能优势是特定于编译器的。使用++计数器是安全的,并习惯它
检查大小是否有一个非0的值,并在此处返回一个默认值我选择了0
顺便说一句,如果这是一个看起来正确的练习,那么可以使用原始形式的指针和数组。但最好用于这种实现。double是一个单值,您不能索引double。如果你想要一个数组,因为这个问题是用C++标记的,或者:
双getHighestRainfallconst std::vector和ArrayOfNumber
然后arrayOfNumbers.size保存向量中的元素数,因此不需要将此数作为参数传递。然后循环将为forint count=0;计数可能大小是指数组中的元素数?然后使用size\u t作为类型,因为double是一个浮点数,这在这里没有意义。然后循环需要为forint count=0;计数<大小;count++错误消息中有什么不清楚的地方?你不能定义没有大小的数组,getLowestValue似乎是未知的,可能缺少原型,ArrayOfNumber是双精度的,不是指针,因此你不能对它进行索引。请使用。添加了更多程序块,请检查我不能使用任何指针,因为我们没有涵盖它们yetyes,大小是指数组中的元素数
double getHighestRainfall(double arrayOfNumbers[], int SIZE)