我知道这个问题已经发布在其他地方了,我已经尝试过这些解决方案了,但是我得到错误LNK1561并且不知道它导致了什么问题。该程序计算并打印用户输入的数字序列的最大值,最小值,平均值和总和。如有任何问题,或者您需要更多信息,请询问。
#include <iostream>
#include <climits>
using namespace std;
template <class T>
T dataSet(T &sum, T &largest, T &smallest, T avg);
template <class T>
int main(){
cout << "This program calculates and prints the largest, smallest,"
<< endl << "average, and sum of a sequence of numbers the user enters." << endl;
T avg, sum, largest, smallest;
avg = dataSet(&sum, &largest, &smallest, avg);
cout << "The largest of the sequence you entered is: " << largest << endl;
cout << "The smallest of the sequence you entered is: " << smallest << endl;
cout << "The sum of the sequence you entered is: " << largest << endl;
cout << "The average of the sequence you entered is: " << avg << endl;
return 0;
}
template <class T>
T dataSet(T &sum, T &largest, T &smallest, T avg){
T num;
signed long long int max = LLONG_MIN, min = LLONG_MAX;
int count;
do{
cout << "Enter a sequence of numbers: (^Z to quit) ";
cin >> num;
if(cin.good()){
count++;
sum += num;
if(num > max)
max = num;
if(num < min)
min = num
}
else if(!cin.good()){
cout << "Error. Try Again.";
}
}while(!cin.eof());
avg = sum / count;
return avg;
}
答案 0 :(得分:1)
main()
不能是模板。您必须在其前面放置template <class T>
行,并使用特定类型(例如dataSet
)实例化double
,例如:
// No template <class T line>
int main(){
cout << "This program calculates and prints the largest, smallest,"
<< endl << "average, and sum of a sequence of numbers the user enters." << endl;
double avg, sum, largest, smallest;
avg = dataSet(sum, largest, smallest, avg);
…