class "ArrayList" may not have a template argument list

0

Cześć, napisałem tablicową implementacje listy i chciałem użyć do niej szablonów klasy ale średnio to rozumiem i działając zgodnie z tutorialem mam problem który wygląda następująco:

template < class T >
class ArrayList
{
    public:
        T _counter;
        T _lastElementsOfArray; 
        T _elements[_maxlengthOfArray];
};

template < class T >
T First(ArrayList  _arrayList)//zwraca pozycję elementu pierwszego -(indeks 0).
{
    return 0;
}
///
/.
/.
/.
///
int main()
{
    ArrayList <int> _arrayList;

    cout<<"#1"<<endl;
    _arrayList._lastElementsOfArray = -1;//make arraylist empty and clean without it arraylist is starting from 0
    Insert<int>(100, First<int>(_arrayList), _arrayList);//using Insert
    print<int>(_arrayList);

    cout<<"#2"<<endl;
    Insert<int>(10, First<int>(_arrayList), _arrayList);//using First, End, Insert
    Insert<int>(1, End<int>(_arrayList), _arrayList);
    print<int>(_arrayList);

    cout<<"#3"<<endl;
    int value = Locate<int>(10, _arrayList);//Using Locate and Delete
    Delete<int>(value, _arrayList);
    Locate<int>(value, _arrayList);
    print<int>(_arrayList);

    cout<<"#4"<<endl;
    Insert<int>(1000, Previous<int>(End<int>(_arrayList), _arrayList), _arrayList);
    print<int>(_arrayList);
}

Ogólnie przed zastosowaniem szablonów wszystko działało prawidłowo jednak teraz otrzymuję błąd w int main() w linice ArrayList <int> _arrayList; który brzmi: class "ArrayList" may not have a template argument list

Moźe mi ktoś wyjaśnić co robię źlę ?

0

Gdzie deklaracja i inicjalizacja _maxlengthOfArray?

0

Wrzuciłem tylko część kodu, wrzucam już całość żeby było łatwiej:

/*

Tablicowa implementacja listy

First() - zwraca pozycję elementu pierwszego -(indeks 0).
END() - zwraca pozycję za ostatnim elementem (czyli last+1).
Next(p) -zwraca indeks następnego elementu po p (czyli p+1): -jeśli istnieje element następny, to zwróć jego indeks -jeśli nie istnieje element następny, to zwróć -1.
Previous(p) - zwraca indeks poprzedniego elementu w stosunku do p (czyli p-1) -jeśli istnieje element poprzedni, to zwróć jego indeks -jeśli nie istnieje element poprzedni, to zwróć -1.
Insert(x,p) - wstawia x do komórki o numerze p ( o ile się da): -sprawdź, czy jest miejsce w tablicy -sprawdź, czy pozycja p jest poprawna -przesuń elementy w tablicy, aby komórka p była pusta -wstaw x do komórki p -zwiększ last -zwróć true gdy operacja wstawiania się powiedzie, false wpp.
Delete(p) - usuwa element z komórki o numerze p: -sprawdź, czy pozycja p jest poprawna -przesuń elementy w "górę" tablicy -zmniejsz last -zwróć true gdy operacja usuwania się powiedzie, false wpp.
Locate(x) - zwraca pozycję elementu x w liście (indeks komórki), jeśli x występuje w tablicy. -zwraca END() (pozycję za ostatnim elementem) gdy x nie występuje w tablicy.
Retrieve(p) - zwraca element znajdujący się w liście na pozycji p(w komórce p), jeśli pozycja p jest poprawna - zwraca MIN wpp, gdzie MIN to wartość która na pewno nie wystąpi w naszej liście. Gdy przechowujemy w liście liczby integer np. MIN=-10000000.

*/


#include <iostream>
#include <cstdio>
#include <iomanip>

using namespace std;


const int _maxlengthOfArray = 100;

template < class T >
class ArrayList
{
    public:
        T _counter;
        T _lastElementsOfArray; 
        T _elements[_maxlengthOfArray];
};

template < class T >
T First(ArrayList  _arrayList)//zwraca pozycję elementu pierwszego -(indeks 0).
{
    return 0;
}

template < class T >
T End(ArrayList _arrayList)//zwraca pozycję za ostatnim elementem (czyli last+1).
{
    return _arrayList._lastElementsOfArray + 1;
}

template < class T >
int Next(int p, class ArrayList _arrayList)//zwraca indeks następnego elementu po p (czyli p+1)
{
    if (p < End(_arrayList))
        return p + 1;
    else
        cout << "WRONG OPERATION" << endl;
}

template < class T >
int Previous(int p, class ArrayList _arrayList)//zwraca indeks poprzedniego elementu w stosunku do p (czyli p-1)
{
    if ((p <= First(_arrayList)) || (p > End(_arrayList)))
        cout << "WRONG OPERATION" << endl;
    else
        return (p - 1);
}

template < class T >
bool Insert(int x, int p, ArrayList &_arrayList)//wstawia x do komórki o numerze p ( o ile się da)
{
    if ((p < 0) || (p > End(_arrayList))) return false;

    if (p == End(_arrayList))
    {
        _arrayList._elements[p] = x;
        _arrayList._lastElementsOfArray = _arrayList._lastElementsOfArray + 1;
        return true;
    }
    if (p < End(_arrayList))
    {
        for (int k = End(_arrayList); k > p; k--)
        {
            _arrayList._elements[k] = _arrayList._elements[k - 1];
        }
        _arrayList._elements[p] = x;
        _arrayList._lastElementsOfArray = _arrayList._lastElementsOfArray + 1;
        return true;
    }
    return false;
}

template < class T >
bool Delete(int p, ArrayList &_arrayList)//usuwa element z komórki o numerze p: -sprawdź, czy pozycja p jest poprawna
{
    while (p < End(_arrayList) - 1)
    {
        _arrayList._elements[p] = _arrayList._elements[p + 1];
        p++;
    }
    _arrayList._lastElementsOfArray--;
    return false;
}

template < class T >
int Locate(int x, ArrayList _arrayList)//zwraca pozycję elementu x w liście (indeks komórki), jeśli x występuje w tablicy. -zwraca END()
{
    for (int i = 0; i < End(_arrayList); i++)
        if (_arrayList._elements[i] == x) return i;
    return End(_arrayList);
}

template < class T >
int Retrieve(int p, ArrayList _arrayList)//zwraca element znajdujący się w liście na pozycji p(w komórce p), jeśli pozycja p jest poprawna
{
    if ((p >= 0) || p <= End(_arrayList)) return _arrayList._elements[p];
}

template < class T >
void print(ArrayList _arrayList)//function printing value of arraylist
{
    int i = First(_arrayList);
    while (i != End(_arrayList))
    {
        cout<<Retrieve(i, _arrayList)<<" ";
        i = Next(i, _arrayList);
    }
    cout<<endl;
}

int main()
{
    ArrayList <int> _arrayList;

    cout<<"#1"<<endl;
    _arrayList._lastElementsOfArray = -1;//make arraylist empty and clean without it arraylist is starting from 0
    Insert<int>(100, First<int>(_arrayList), _arrayList);//using Insert
    print<int>(_arrayList);

    cout<<"#2"<<endl;
    Insert<int>(10, First<int>(_arrayList), _arrayList);//using First, End, Insert
    Insert<int>(1, End<int>(_arrayList), _arrayList);
    print<int>(_arrayList);

    cout<<"#3"<<endl;
    int value = Locate<int>(10, _arrayList);//Using Locate and Delete
    Delete<int>(value, _arrayList);
    Locate<int>(value, _arrayList);
    print<int>(_arrayList);

    cout<<"#4"<<endl;
    Insert<int>(1000, Previous<int>(End<int>(_arrayList), _arrayList), _arrayList);
    print<int>(_arrayList);
}
1

Radzę najpierw zwykle funkcje opanować.
Co te funkcje zwrócą po wyświetleniu "WRONG OPERATION"?

Baxing napisał(a):
template < class T >
int Next(int p, class ArrayList _arrayList)//zwraca indeks następnego elementu po p (czyli p+1)
{
    if (p < End(_arrayList))
        return p + 1;
    else
        cout << "WRONG OPERATION" << endl;
}

template < class T >
int Previous(int p, class ArrayList _arrayList)//zwraca indeks poprzedniego elementu w stosunku do p (czyli p-1)
{
    if ((p <= First(_arrayList)) || (p > End(_arrayList)))
        cout << "WRONG OPERATION" << endl;
    else
        return (p - 1);
}

Czyli plan:

  • funkcje
  • klasy
  • wyjątki
  • wzorce

1 użytkowników online, w tym zalogowanych: 0, gości: 1