Как инициировать массив generi c в шаблонном классе? - PullRequest
0 голосов
/ 29 марта 2020

У меня есть код драйвера и класс CTest, и я должен использовать CTest в качестве аргумента типа. Это код инициализации:

TSimpleStorage<CTest> store(iSize);

, но у меня возникает ошибка при запуске этого

for(int i = 0; i<iSize; i++) {
    store.Add(new CTest(i));
}

В соответствии с ошибкой, в моей IDE я не могу сохранить экземпляр класса, потому что массив уже является указателем на тип CTest. Как это исправить?

template <class Type>
    class TSimpleStorage
    {
    private:
        Type *ptrArray;
        int maxSize;
        int CurrentElement = 0;
    public:
        void incrementElement();
        TSimpleStorage(int);
        void Add(Type*);
        void Delete(int);
        TSimpleStorage operator [](int);
    };
    template <class Type>
    TSimpleStorage<Type>::TSimpleStorage(int listSize)
    {
        maxSize = listSize;
        ptrArray[maxSize];
    }



    template <class Type>
    void TSimpleStorage<Type>:: Add(Type *item)
    {
       for (int i = 0; i < maxSize; i++)
       {
          ptrArray[i] = item;
      }

    }

    template <class Type>
    void TSimpleStorage<Type>:: Delete(int delet)
    {
        for (int i = 0; i < maxSize; i++)
        {
            if (ptrArray[i] == item)
            {
                ptrArray[i] = '\0';
                break;
            }
        }
    }

    template <class Type>
    TSimpleStorage<Type> TSimpleStorage<Type>:: operator [](int index)
    {
        assert(0 <= index && index < maxSize);

        return ptrArray[index];

    }


    class CTest
    {
    private:
        const int iTd;
    public:
        CTest(const int i) :iTd(i){}
        CTest();


        friend ostream& operator << (ostream& out, const CTest* c)
        {
            cout << (c != NULL ? c->iTd : -1 );
            return out;
        }
    };


    int main(int arc, char* argv[])
    {
        int iSize = 0;
        cout << "Please enter the size of the storage:";
        cin >> iSize;
        cout << endl;

        //Allocate...
        TSimpleStorage<CTest> store(iSize);

        //Add to the TSimpleStorage...
        for(int i = 0; i<iSize; i++)
            store.Add(new CTest(i));

        //Test the delete operation...
        int iDelete = 0;
        cout << "Please enter the index of the item to delete:";
        cin >> iDelete;
        cout << endl;

        //Get the instance of CTest that corresponds to the index the user entered
        CTest* t = iDelete;
        cout << "CTest which contains the id of "<< t<<"is going to be deleted!"<< endl;

        //The instance will be deleted in TSimpleStorage
        //Note: It is NOT the index that is passed into the function.
        store.Delete(t);

        //Print out all of the CTest instances (included the delted instance).
        for(int i = 0; i< iSize; i++)
            cout <<"CTest has an ID of: "<< store[i] << endl;

        system("pause");

        return 0;
    }
...