[英]What does expression must have a pointer to object type mean in my code?
this is the function that's coming up with the most errors这是出现最多错误的 function
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;
}
the errors:错误:
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];
~~~~~~~~~~~~~~^~~~~~
A double
is a single value and you cannot index a double. double
精度值是单个值,您不能索引双精度值。 If you want an array (since the question is tagged C++) use std::vector or std::array :如果您想要一个数组(因为问题被标记为 C++),请使用std::vector或std::array :
double getHighestRainfall(const std::vector<double> &arrayOfNumbers)
arrayOfNumbers.size()
then holds the number of elements in the vector, so you don't need to pass this number along as parameter. arrayOfNumbers.size()
然后保存向量中元素的数量,因此您不需要将此数字作为参数传递。 Then the loop will be for(int count = 0; count < numberOfArrays.size(); count++)
然后循环将是
for(int count = 0; count < numberOfArrays.size(); count++)
If you are allowed to use std::vector
then use it and skip the rest of answer.如果您被允许使用
std::vector
然后使用它并跳过 rest 的答案。 If you are for some reason not allowed to use std::vector
you need to use a pointer:如果由于某种原因不允许使用
std::vector
,则需要使用指针:
double getHighestRainfall(const double *arrayOfNumbers, size_t size)
Probably SIZE
was meant to be number of elements in the array?可能
SIZE
是数组中元素的数量? Then you use size_t
as type, because double
is a floating point number, which makes no sense here.然后你使用
size_t
作为类型,因为double
是一个浮点数,在这里没有意义。 Then the loop needs to be for(int count = 0; count < size; count++)
那么循环需要是
for(int count = 0; count < size; count++)
The issue is that arrayOfNumbers
is not a pointer but a double
, indexing it is a non-sense.问题是
arrayOfNumbers
不是指针而是double
,索引它是无意义的。
Moreover, your for
loop condition is wrong.此外,您的
for
循环条件是错误的。 You need to iterate over all elements of the array.您需要遍历数组的所有元素。
What you probably wanted to write:你可能想写的:
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.
}
}
I have added a checking over the array size in order to avoid to explicitly access unreserved memory location.我添加了对数组大小的检查,以避免显式访问未保留的 memory 位置。
Note: You probably have to use std::array
instead of raw arrays.注意:您可能必须使用
std::array
而不是原始 arrays。 If you need a dynamic array, then std::vector
would suit your needs.如果您需要一个动态数组,那么
std::vector
将满足您的需求。
You are getting that error because you have to pass a pointer to your array instead.您收到该错误是因为您必须改为将指针传递给您的数组。 Here is the correct one
这是正确的
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;
}
As you said you did not cover pointers yet you can define your function like this.正如你所说,你没有涵盖指针,但你可以像这样定义你的 function 。 But it's the same as passing the pointer under the hood and does not make a big difference.
但这与在引擎盖下传递指针相同,并没有太大区别。
double getHighestRainfall(double arrayOfNumbers[], int SIZE)
Some hints:一些提示:
SIZE should be int because array length is an integer ( actually it should be unsigned int) SIZE 应该是 int 因为数组长度是 integer (实际上它应该是 unsigned int)
use ++count in for-loop for performance reasons.出于性能原因,在 for 循环中使用 ++count。 (it's compiler-specific if it has performance benefits or not. Use ++counter to be in the safe side and get used to it)
(它是否具有性能优势是特定于编译器的。使用 ++counter 以确保安全并习惯它)
By the way, if this is an exercise (which looks to be true) that is ok to use pointers and arrays in their raw form.顺便说一句,如果这是一个练习(看起来是真的),那么可以使用原始形式的指针和 arrays。 But it is better to use std::vector for such implementation.
但最好使用std::vector进行此类实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.