3

I'm trying to recreate the inbuilt vector class in cpp to get a bit more practice with classes and memory management. I keep getting an error that says 'ISO C++ forbids declaration of ‘allocator’ with no type' and I can't figure out why for the life of me. Is there something I'm missing?

#include <cstddef>
#include <memory>

template <class T>
class myvector{
public:
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef size_t size_type;

    myvector(){ data = avail = limit = 0; }

    explicit myvector(size_type n, const T& t = T()) { create(n,t); }

    myvector(const myvector& v){  create(v.begin(), v.end());  }

    ~myvector() {  uncreate();  }

    myvector& operator=(const myvector& v)
    {
        if (&v != this){
            uncreate();
            create(v.begin(), v.end());
        }
        return *this;
    }


    T& operator[](size_type i) { return data[i];  }
    const T& operator[](size_type i) const {  return data[i];  }

    iterator begin(){  return data;  }
    const_iterator begin() const{  return data;  }

    iterator end(){  return limit;  }    
    const_iterator end() const{  return limit;  }

    size_type size(){  return avail - data;  }

    void push_back(T t)
    {
        if(avail == limit)
            size_type new_size = max(2*(limit-data),ptrdiff_t(1));
            iterator new_data = alloc.allocate(new_size);
            iterator new_avail = uninitialized_copy(data,avail,new_data);
            uncreate();
            data = new_data;
            avail = new_avail;
            limit = data + new_size;
        alloc.construct(avail++,t);
    }
private:
    iterator data;
    iterator avail;
    iterator limit;
    allocator<T> alloc;

    void create(size_type n, const T& t)
    {
        data = alloc.allocate(n);
        limit = avail = data+n;
        uninitialized_fill(data,limit,t);
    }

    void create(const_iterator i, const_iterator j)
    {
        data = alloc.allocate(j-i);
        limit = avail = uninitialized_copy(i,j,data);
    }

    void uncreate()
    {
        if(data){
            iterator it = avail;
            while(it != data) {   alloc.destroy(--it);    }
            alloc.deallocate(data,limit-data);
        }
        data = limit = avail = 0;
    }
};
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
hedgehogrider
  • 1,168
  • 2
  • 14
  • 22
  • Crap, I can never get code formatting right on this website... The first statements are supposed to be "#include " and "#include " – hedgehogrider Dec 30 '10 at 01:25
  • 1
    code is indented by four spaces. That's all you need. Press the `{}` button once you've pasted your code in. I've done it on the above so you can see. I've not changed the indenting in any other way so if there's any errors, you can edit your question to correct what I've done. –  Dec 30 '10 at 01:37
  • 1
    You should use the [copy-and-swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) for your assignment operator. – GManNickG Dec 30 '10 at 01:44
  • This is chapter 11 of "Accelerated C++", isn't it? – Mattia Sep 09 '15 at 15:33

2 Answers2

3

It should be, std::allocator<T> alloc;, everything in the standard library is contained within the namespace std.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
0

Alos see http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h. It includes work arounds for GCC bugs.

Note: the standard C++ containers only have to check for overflow on vector::resize (hopefully that has changed by now). The ESAPI allocator checks during all allocations.

jww
  • 97,681
  • 90
  • 411
  • 885