Создание собственного векторного класса, много ошибок

0

Я новичок в C++ и создаю классы, использующие его, особенно используя классы шаблонов. Я получаю много сообщений об ошибках, и я не уверен, как их исправить, надеялся, что кто-то может помочь, спасибо заранее.

Класс:

template <class T> class Vector {
    public:
        typedef T* iterator;
        Vector();
        iterator begin();
        iterator end();
        int size();
        iterator insert(iterator position, const T& item);
        void alloc_new();

    private:
        T items;
        int used;      
};

template <class T> Vector::Vector() { 
    items = [1000];
    used = 0;
}

template <class T> iterator Vector::begin() {
   return items; 
}

template <class T> iterator Vector::end(){
    return items + used;
}

template <class T> int Vector::size() {
    return used;
}

template <class T> iterator Vector::insert(iterator position, const T& item){

    if (used+1 > items){
        alloc_new();}  

    items[position] = item;
    used =+ 1;

}

template <class T> void Vector::alloc_new(){
        items = used*2;
        T tmp[] = items;

        for (int i = 0; i < used; i++){
            tmp[i] = items[i];
        } 
        delete items;
        items = tmp;
    }

Сообщения об ошибках:

main.cpp:67: error: 'template class Vector' used without template parameters
main.cpp:67: error: ISO C++ forbids declaration of 'Vector' with no type
main.cpp:67: error: declaration of template 'template int Vector()'
main.cpp:52: error: conflicts with previous declaration 'template class Vector'
main.cpp:52: error: previous non-function declaration 'template class Vector'
main.cpp:67: error: conflicts with function declaration 'template int Vector()'
main.cpp: In function 'int Vector()':
main.cpp:68: error: 'items' undeclared (first use this function)
main.cpp:68: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:68: error: expected primary-expression before '[' token
main.cpp:69: error: 'used' undeclared (first use this function)
main.cpp: At global scope:
main.cpp:72: error: expected constructor, destructor, or type conversion before "Vector"
main.cpp:72: error: expected ';' before "Vector"
main.cpp:76: error: expected constructor, destructor, or type conversion before "Vector"
main.cpp:76: error: expected ';' before "Vector"
main.cpp:80: error: 'template class Vector' used without template parameters
main.cpp: In function 'int size()':
main.cpp:81: error: 'used' undeclared (first use this function)
main.cpp: At global scope:
main.cpp:84: error: expected constructor, destructor, or type conversion before "Vector"
main.cpp:84: error: expected ';' before "Vector"
main.cpp:94: error: 'template class Vector' used without template parameters
main.cpp: In function 'void alloc_new()':
main.cpp:95: error: 'items' undeclared (first use this function)
main.cpp:95: error: 'used' undeclared (first use this function)
Теги:
class
vector

3 ответа

1

Вам нужно указать параметры шаблона. Например:

template <class T> Vector<T>::Vector() { 
    items = [1000];  // note, this is also invalid syntax
    used = 0;
}
1
  1. Vector::vector() необходимо изменить на Vector<T>::vector(). Итак, следующие функции-члены.
  2. iterator должен предшествовать Vector<T> качестве Vector<T>::iterator так как iterator объявлен внутри класса Vector
  • 0
    Это верно? template <class T> Vector<T>::iterator Vector<T>::end()
  • 0
    Да: шаблон <typename T> Vector <T> :: iterator Vector <T> :: end () правильный, но, конечно, вам нужно знать размер вектора.
0

поскольку итератор является вложенным типом вектора, изменение

template <class T> iterator Vector::begin() { return items; }

в

template <typename T> Vector<T>::iterator Vector<T>::begin() { return items; }

Сделайте это со всеми другими методами, использующими итератор.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню