【问题标题】:Error: invalid conversion from ‘int*’ to ‘int’ when attempting to pass arrays错误:尝试传递数组时从“int*”到“int”的无效转换
【发布时间】:2018-10-02 05:30:40
【问题描述】:

我在一个 C++ 课程中,对于一个项目,我们需要使用数组计算成绩,并且在 main 之外至少有 3 个函数。 到目前为止我的代码:

#include <iostream>

using namespace std;

double gradeAverage(double, double, double, double);
double letterGrade(double);
double arrayAverage(int, int);
void droppedQuizzes(int, int);


int main(){
    int quizGrades[7] = {100, 0, 50, 30, 40, 100, 0};
    int testGrades[2] = {50, 54};
    int projectGrades[4] = {100, 0, 90, 95};
    int labGrades[6] = {100, 100, 0, 50, 60, 100};
    int newQuizGrades[5] = {0, 0, 0, 0, 0};



    droppedQuizzes(quizGrades, newQuizGrades);


    double quizAve = arrayAverage(quizGrades, 7);
    double testAve = arrayAverage(testGrades, 2);
    double projAve = arrayAverage(projectGrades, 4);
    double labAve = arrayAverage(labGrades, 6);

    double gradeAve = gradeAverage(quizAve, testAve, projAve, labAve);

    char finalGrade = letterGrade (gradeAve);

    cout << "Final Numeric Average: " << gradeAve << endl << "Letter Grade: " << finalGrade << endl;

    return 0;
}

double gradeAverage(double quiz, double test, double proj, double lab){
    double quizWeighted = quiz * 0.2;
    double testWeighted = test * 0.25;
    double projWeighted = proj * 0.2;
    double labWeighted = lab * 0.15;
    double finalWeighted = 0.2; // assuming 100 as the score.

    return (quizWeighted + testWeighted + projWeighted + labWeighted + finalWeighted);
}

double LetterGrade(double ave){
    if(ave >= 90)
        return 'A';
    else if(ave >= 80)
        return 'B';
    else if(ave >= 70)
        return 'C';
    else if(ave >= 65)
        return 'D';
    else
        return 'F';
}

double arrayAverage(int arr[], int size){
    double sum = 0;
    for(int x = 0; x < size; x++){
        sum += arr[x];
    }
    return (sum / size);
}

void droppedQuizzes(int quizzes[7], int newQuizzes[5]){
    int low1 = 100, low2 = 100, count = 0;


    for(int x = 0; x < 7; x++){
        if(quizzes[x] < low2)
            if(quizzes[x] < low1)
                low1 = quizzes[x];
            else
                low2 = quizzes[x];
        else{
            newQuizzes[count] = quizzes[x];
            count++;
        }
    }
}

我得到了很多错误,但他们都说同样的话,关于不同的变量:

Lab6.cpp:20:42: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
  droppedQuizzes(quizGrades, newQuizGrades);
                                          ^
Lab6.cpp:8:6: error:   initializing argument 1 of ‘void droppedQuizzes(int, int)’ [
 void droppedQuizzes(int, int);
      ^

我知道*代表一个指针,但我不明白为什么我不能传递一个数组,我之前在其他代码中做过。

将不胜感激一个解决方案,以及更多的解释。谢谢!

【问题讨论】:

  • 你应该创建一个minimal reproducible example,这样问题就很明显了。
  • 程序顶部的函数原型与底部的函数定义不匹配。

标签: c++ arrays pointers int


【解决方案1】:

函数原型必须指定与函数定义相同的签名。所以这些行:

double arrayAverage(int, int);
void droppedQuizzes(int, int);

应该是:

double arrayAverage(int[], int);
void droppedQuizzes(int[], int[]);

【讨论】:

  • 谢谢!虽然我还有另一个问题。我的印象是这只适用于 Java,并且括号需要与变量名相邻。不是这样吗,还是这里发生了其他事情?
  • 在原型中,名称是可选的。您需要定义中的名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多