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 に答える