2

ベクトルを作成し、それにバブルソートを使用するクラスを作成しようとしています。バブルと呼ばれる BubbleStorage クラスを作成しようとした場合を除いて、すべて正常にコンパイルされます。

コンパイラは、「バブルの前にテンプレート引数がありません」、「バブルの前に予想されます」というエラーを表示します。

このコードは完成していません。ただし、私はまだバブルソート機能を作成中です。先に進む前に、これを処理したいだけです。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <vector>

using namespace std;

template<typename T>
class BubbleStorage
{
public:
    BubbleStorage();
    ~BubbleStorage();
    vector<T>MyVector;

    void add_data(int size)
    {
        srand (time(NULL));

        for (T i = 0; i <= size; i++)
            random = rand() % 100;
        MyVector.push_back(random);
    }

    void display_data()
    {
        cout<<"The Vector Contains the Following Numbers"<<endl;
        for (vector<int>::iterator i = MyVector.begin(); i != MyVector.end(); ++i)
            cout<<' '<< *i;
    }

    void max()
    {

    }

    void min()
    {

    }
};

int main(int argc, char *argv[])
{
    srand (time(NULL));

    int size = rand() % 50 + 25;
    BubbleStorage bubble;

    bubble.add_data(size);
    bubble.display_data();

}
4

1 に答える 1

2

BubbleStorage はテンプレート化されたクラスであり、テンプレート引数が必要です。

試す

BubbleStorage<int> bubble;

また、このテンプレート引数が与えられた場合、クラス関数で、"int" または "double" または "MyClass" でさえテンプレート パラメーターである T を使用すると想定しないようにしてください。したがって、ベクトルのイテレータが必要な場合は、

vector<T>::iterator //or
vector<T>::const_iterator

では、それが int 変換可能add_dataであると想定すべきではありません。ランダムなsTを取得するための外部関数が必要です。Tこれらの問題を考慮して、実際にテンプレート化する必要があることを確認してくださいBubbleStorage。または、ベクトルの代わりにサイズをadd_data取得します。T

于 2013-09-15T18:38:23.103 に答える