首页 > 解决方案 > 在我的代码中,表达式必须有一个指向对象类型的指针是什么意思?

问题描述

这是出现最多错误的函数

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];
              ~~~~~~~~~~~~~~^~~~~~

标签: c++

解决方案


Adouble是单个值,您不能索引双精度值。如果您想要一个数组(因为问题被标记为 C++),请使用std::vectorstd::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_tas 类型,因为double是浮点数,这里没有意义。然后循环需要for(int count = 0; count < size; count++)


推荐阅读