【问题标题】:C++ simple calculate score with arraysC++ 用数组简单计算分数
【发布时间】:2013-06-30 04:02:00
【问题描述】:

这是我教科书中计算分数的练习作业。它需要 7 分,并下降最高和最低。

我认为没有语法错误,但我收到了未解决的外部符号错误。我寻找了类似的问题,似乎问题可能出在使用函数但没有定义它。我已经定义了我的所有函数,但可能在 main 或 calculatescore 中都不正确。我是 C++ 新手,希望能在解决此问题方面提供任何帮助。谢谢

这是我在 VisualStudio 上遇到的错误

错误 LNK2019:函数 _main 中引用了无法解析的外部符号“float __cdecl calculateScore(float * const,int,float)”(?calculateScore@@YAMQAMHM@Z)

#include <iostream>
using namespace std;

void printHeader(int judges);
void enterData (float scores[], int judges, float difficulty);
float calculateScore(float scores[], const int judges, float difficulty);
int findLeast(float scores[], const int judges);
int findMax(float scores[], const int judges);

int main () {
    const int judges = 7;
    float scores [judges];
    float difficulty = 0;
    float finalscore = calculateScore(scores, judges, difficulty);



printHeader (judges);
    enterData (scores, judges, difficulty);  // get user input
    findLeast(scores, judges); // find lowest score 
    findMax(scores, judges); // find highest score
    calculateScore (scores, judges, difficulty); // get final score
    cout << "The final score is " << finalscore << '\n';

    return 0;
}

void printHeader(const int judges) {
    cout << "This program calculates a divers score over" << judges << "judges"; 
}

void enterData(float scores[], const int judges, float difficulty) {
    for (int i = 0; i < judges; i++){
        cout <<"Enter score for judge " << i+1 << endl; 
        cin >> scores[i];
    }
    cout << "Enter difficulty: "<< endl;
    cin >> difficulty;
}

这是我在 main 中调用的计算分数的函数。它应该是一个 void 函数吗?

float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least) {
    float sum = 0;
        for (int i = 0; i < judges; i++) {
        sum += scores[i];
    }
    return sum - scores[least] - scores[maxScore] * difficulty * .6;
}


int findLeast(float scores[], const int judges) {
    int least = 0;
    for (int i = 1; i< judges; i++)
        if (scores[i] < scores[least])
            least = i;
    return least;
}

int findMax(float scores[], const int judges) {
    int maxScore = 0;
    for (int i = 1; i< judges; i++)
        if (scores[i] > scores[maxScore]) {
            maxScore = i; 
        }
    return  maxScore;
}

【问题讨论】:

    标签: c++ arrays function calculator


    【解决方案1】:

    您在 main 函数中使用 args float * const,int,float 调用 calculateScore,而实际方法的签名是 float scores[], const int judges, float difficulty, int maxScore, int least

    总之,您在调用main() 中的函数时缺少参数。该方法需要一个数组scores[]、一个整数judges、一个浮点数difficulty、一个整数maxScore,以及一个整数least,但你只给出了一个float*(不是与 float[])、int 和另一个 float 相同。

    因此,您没有提供正确的参数类型/数量,编译器告诉您它找不到具有这些参数类型的方法。

    与您的错误无关的一个问题是,在您的方法定义(calculateScore)中,您使用maxScore 作为scores 的索引,而maxScore 似乎只是最高分的值。因此,您可以将scores[maxScore] 替换为简单的maxScoreleast 也是如此 - 它是一个值,而不是索引,因此您可以使用 least 而不是 scores[least]

    【讨论】:

    • @Yve 不想和你争论!我已经编辑了我的答案以使其更清楚。
    • 哦,我认为我的问题是我必须将 [] 放在函数调用中(同时不列出所有变量)
    • @Marla 基本上这里的问题是你没有给它正确的信息。您拥有可以传入的变量中的信息,因此您只需更改调用方法的方式。
    • 原型也是错误的,如果你也能更新你关于这个问题的答案会很好。
    • 好的,谢谢。必须在我的计算中解决更多问题,但解决函数调用令人放心
    【解决方案2】:

    此签名适用于您编写的函数:float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least)

    这是你的原型:float calculateScore(float scores[], const int judges, float difficulty);

    它们不匹配的是签名。

    首先你应该将你的原型更改为:

    float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least);

    正如已经指出的那样,您应该使用 5 个类型的变量调用您的函数:float []const intfloatintint 而不是 float []const int、@987654331 @

    我认为这是你想要的主要内容:

    int max = findMax(scores, judges);
    int min = findLeast(scores, judges);
    calculateScore (scores, judges, difficulty, max, min);
    

    【讨论】:

    • @AmitApollo 谢谢你,但如果我投了反对票,如果有什么问题,我仍然很高兴能理解我的回答有什么问题......
    • 不认识亚历山德鲁。它绝对没有错!这与我的回答相同。
    • @AmitApollo 应该禁止用户之间的代表交换;)
    【解决方案3】:

    将 main 中的代码重写为以下内容:

      const int judges = 7;
      float scores [judges];
      float difficulty = 0;
      float finalscore;
       
      printHeader (judges);
      int leastTemp;
      int maxTemp;
      enterData (scores, judges, difficulty);  // get user input
      leastTemp = findLeast(scores, judges); // find lowest score 
      maxTemp = findMax(scores, judges); // find highest score
      finalscore = calculateScore (scores, judges, difficulty, maxTemp, leastTemp); // get final score
      cout << "The final score is " << finalscore << '\n';
    
      return 0;
    

    这样您就可以将两个缺失的参数放入函数 calculateScore。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多