So I'm writing a memory pool allocator template where it assumes all the objects are of constant size. I need it for a couple objects in a game i'm writing because the dynamic allocation of a large number of small objects is more slow than i'd like.

My issue is i've been trying to write with a more modern c++ style recently so trying to use vectors and the stl more than just writing my own structures all the time.

However, i get an error in the constructor and in my resize function whenever I do the following code.

error C2664: 'void std::vector<_Ty>::push_back(std::vector<LinkedListNode<T>> *&&)' : cannot convert parameter 1 from 'std::vector<_Ty> *' to 'std::vector<_Ty> *&&'
1>

I don't know what to do to get this to append.

Edit: solved it. Got my (*) mixed up when making templates. always helps to get on here and write out the problem for some reason.

template<typename T>
class ObjectPool
{

private:
    static const int stride = sizeof(LinkedListNode<T>);
    static const int ptr_size = sizeof(LinkedListNode<T>*);
    int mMax;
    int mTotal;
    int mAllocations; //Keep the count of number of allocated objects
    int mFree; //Keep the count of non-allocated values
    int mMinimumExpansion;

    vector< vector< LinkedListNode<T> >* > memory_pool;
    LinkedListNode<T>* front; //front value is the one that is used during allocations 
    LinkedListNode<T>* back; //back value is where values are returned
public:

    //... lots of functions pass over that for now



ObjectPool(int size) : mTotal(size),mAllocations(0),mFree(size),mMax(0),mMinimumExpansion(10)
    {
        if(size < 1)
            throw std::exception("A memory pool cannot be initialized to a size less than one");

        //make the new vector 
        memory_pool = vector< vector<LinkedListNode<T> >*>(1);
        //make our first memory pool
        vector<LinkedListNode<T>*>* pool = new vector<LinkedListNode<T>*>();
        ptr->resize(size);
        memory_pool.push_back(ptr); //ERROR: C2664

        front = pool->data(); //make the first member of the pool the front of the list

        //this iterates through the memory pool and links the nodes
        LinkedListNode<T>* prev = front;
        for(int i = 1;i < size;++i)
        {
            LinkedListNode<T>* current = front+i;
            prev->next = current;
            prev = current;
        }

        //format the last node in the list
        prev->next = nullptr;
        back = prev;
    }

glad you got it. please remember to mark this solved thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.