Cosmo D
Cosmo D

Reputation: 845

Having problems using a template container's constructor c++

I'm tryin to use a custom container, and in that container's constructor i pass a memory pool allocator. The whole thing starts like this:

AllocatorFactory alloc_fac;

//Creates a CPool allocator instance with the size of the Object class
IAllocator* object_alloc = alloc_fac.GetAllocator<CPool>(1000,sizeof(Object));
//Creates a CPool allocator instance with the size of the BList<Object> class
IAllocator* list_alloc = alloc_fac.GetAllocator<CPool>(10,sizeof(BList<Object>));
//Same logic in here as well
IAllocator* node_alloc = alloc_fac.GetAllocator<CPool>(1000,sizeof(BListNode<Object>));

The IAllocator class looks like this:

 class IAllocator
{

public:

    virtual void* allocate( size_t bytes ) = 0;
    virtual void deallocate( void* ptr ) = 0;

    template <typename T>
    T* make_new()
    { return new ( allocate( sizeof(T) ) ) T (); }

    template <typename T, typename Arg0>
    T* make_new( Arg0& arg0 )
    { return new ( allocate( sizeof(T) ) ) T ( arg0 ); }

            .......
}

And the container class's constructor looks like this:

template <class T>
class BList {
......
public:
/**
 *@brief Constructor
 */
BList(Allocators::IAllocator& alloc){
     _alloc = alloc;
    reset();
    }
/**
 *@brief Constructor
 *@param inOther the original list
 */
 BList(const BList<T>& inOther){
    reset();
    append(inOther);
    }
.....
}

And when I do this:

 BList<Object> *list = list_alloc->make_new<BList<Object>>(node_alloc);

The compiler complains about this:

Error 1 error C2664: 'Containers::BList::BList(Allocators::IAllocator &)' : cannot convert parameter 1 from 'Allocators::IAllocator *' to 'Allocators::IAllocator &' c:\licenta\licenta-transfer_ro-02may-430722\licenta\framework\framework\iallocator.h 21 Framework

I think i went over my head with this one....

Upvotes: 2

Views: 250

Answers (3)

Useless
Useless

Reputation: 67743

The existing answers are correct, but a quick note on how to read the error yourself: you just have to split it into pieces ...

Error 1 error C2664: 'Containers::BList::BList(Allocators::IAllocator &)' : cannot convert parameter 1 from 'Allocators::IAllocator *' to 'Allocators::IAllocator &'

reads:

  • you're calling Containers::BList::BList(Allocators::IAllocator &), which is a constructor taking one argument, a reference to an IAllocator
  • cannot convert parameter 1 means the compiler is having trouble with the type of that first (and only) argument
    • you've given it this type: ... from 'Allocators::IAllocator *'
    • and it wanted this type (to match the constructor declaration): ... to 'Allocators::IAllocator &'

so, how do you convert from the pointer you have to the reference the constructor wants?


OK, I've added the actual answer as well:

Allocators::IAllocator *node_alloc = // ...
Allocators::IAllocator &node_alloc_ref = *node_alloc;
BList<Object> *list = list_alloc->make_new<BList<Object>>(node_alloc_ref);

or just:

BList<Object> *list = list_alloc->make_new<BList<Object>>(*node_alloc);

Upvotes: 3

Jerry Coffin
Jerry Coffin

Reputation: 490148

Your allocator factory is returning a pointer to an allocator, but your constructor wants a reference to an allocator. You need to dereference the pointer.

IAllocator* node_alloc = alloc_fac.GetAllocator<CPool>(1000,sizeof(BListNode<Object>));    

// Instead of:
// BList<Object> mylist(node_alloc);
// you need:
//
BList<Object> mylist(*node_alloc); 

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

You appear to be calling make_new with a pointer instead of a reference. Try:

BList<Object> *list = list_alloc->make_new<BList<Object>>(*node_alloc);

And please, pick an indentation stile and stick to it.

Upvotes: 1

Related Questions