这是我教科书中的练习作业,用于计算分数。它需要7分,并且最高和最低。
我认为没有语法错误,但我得到一个未解决的外部符号错误。我寻找类似的问题,似乎问题可能是使用函数而不是定义它。我已经定义了所有函数,但在main或calculatecore中可能都不正确。我是c ++的新手,非常感谢您解决这个问题。谢谢
这是我在VisualStudio上得到的错误
错误LNK2019:未解析的外部符号“float __cdecl calculateScore(float * const,int,float)”(?calculateScore @@ YAMQAMHM @ Z)在函数_main中引用
#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中调用的分数的函数。它应该是一个无效函数吗?
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;
}
答案 0 :(得分:5)
您正在使用主函数中的calculateScore
调用float * const,int,float
,而实际方法的签名为float scores[], const int judges, float difficulty, int maxScore, int least
。
总之,您在调用main()
中的函数时缺少参数。该方法需要一个数组scores[]
,一个整数judges
,一个浮点difficulty
和一个int maxScore
,以及一个int least
,但你'我们只提供float*
(与float[]
不同),int
和另一个float
。
因此,您没有提供正确的类型/数量的参数,并且编译器告诉您它无法找到具有这些参数类型的方法。
与您的错误无关的问题是,在您的方法定义(calculateScore)中,您使用maxScore
作为scores
的索引,而maxScore
似乎只是值{最高分。因此,您只需使用scores[maxScore]
替换maxScore
即可。 least
也是如此 - 它是一个值,而不是一个索引,因此您可以使用least
代替scores[least]
。
答案 1 :(得分: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种类型的varibleas调用你的函数:float []
,const int
,float
,int
,int
而不是float []
,const int
,float
我认为你想要的主要是:
int max = findMax(scores, judges);
int min = findLeast(scores, judges);
calculateScore (scores, judges, difficulty, max, min);
答案 2 :(得分:1)
将主内部的代码重写为以下内容:
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。