0

我知道这个问题已在其他地方发布,并且我已经尝试了这些解决方案,但我收到错误 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;
    }
4

1 回答 1

1

main()不能是模板。您必须删除它之前的行,并使用特定类型template <class T>实例化,例如:dataSetdouble

// 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);
…
于 2013-07-17T19:44:13.870 回答