简体   繁体   English

在我的代码中,表达式必须有一个指向 object 类型的指针是什么意思?

[英]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::vectorstd::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 以确保安全并习惯它)

  • check if SIZE has a value other than 0 and return a default value ( here I chose 0)检查 SIZE 是否有非 0 的值并返回一个默认值(这里我选择了 0)

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.

相关问题 C ++“表达式必须具有指向对象类型的指针”和“下标需要数组或指针类型”是什么意思? - What does C++ mean by "expression must have pointer-to-object type" and "subscript requires array or pointer type"? 此错误是什么意思“表达式必须具有指向类的指针类型”? - What is this error mean“ expression must have pointer-to-class type”? “表达式必须具有类类型”错误是什么意思? - What does “expression must have class type” error mean? 表达式必须具有指向对象的类型 - Expression must have pointer-to-object type 表达式必须具有指向对象类型的指针 - Expression must have pointer to object type 表达式必须具有对象指针类型 - the expression must have an object pointer type 错误表达式必须有指向 object 类型的指针 - Error expression must have pointer to object type 为什么赋值表达式中的重叠必须是精确的并且对象必须具有相同的类型,这是什么意思? - Why overlap in an assignment expression must be exact and the objects must have the same type, and what does it mean? 具有函数指针类型是什么意思? - What does it mean to have a function pointer type? 对于c ++中的数组,什么是“表达式必须具有指向对象的指针类型”? - What is “expression must have pointer-to-object type” for arrays in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM