这是我教科书中计算分数的练习作业。它需要 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;
}