【问题标题】:Having problems using a template container's constructor c++使用模板容器的构造函数 C++ 时遇到问题
【发布时间】:2012-05-11 23:06:33
【问题描述】:

我正在尝试使用自定义容器,并且在该容器的构造函数中,我传递了一个内存池分配器。 整个事情是这样开始的:

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>));

IAllocator 类如下所示:

 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 ); }

            .......
}

容器类的构造函数如下所示:

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);
    }
.....
}

当我这样做时:

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

编译器抱怨这个:

错误 1 ​​错误 C2664: 'Containers::BList::BList(Allocators::IAllocator &)' : 无法将参数 1 从 'Allocators::IAllocator *' 转换为 'Allocators::IAllocator &' c:\licenta\ licenta-transfer_ro-02may-430722\licenta\framework\framework\iallocator.h 21 框架

我想我对这个有点过头了....

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    您似乎使用指针而不是引用调用make_new。试试:

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

    请选择一个缩进阶梯并坚持下去。

    【讨论】:

      【解决方案2】:

      您的分配器工厂正在返回一个指向分配器的指针,但您的构造函数需要对分配器的引用。您需要取消对指针的引用。

      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); 
      

      【讨论】:

        【解决方案3】:

        现有的答案是正确的,但是关于如何自己阅读错误的快速说明:您只需将其分成几部分...

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

        阅读:

        • 您正在调用Containers::BList::BList(Allocators::IAllocator &amp;),这是一个带有一个参数的构造函数,一个对IAllocator 的引用
        • cannot convert parameter 1 表示编译器在第一个(也是唯一一个)参数的类型上有问题
          • 你给了它这个类型:... from 'Allocators::IAllocator *'
          • 并且它想要这种类型(以匹配构造函数声明):... to 'Allocators::IAllocator &amp;'

        那么,如何将你拥有的指针转换为构造函数想要的引用?


        好的,我也添加了实际答案:

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

        或者只是:

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

        【讨论】:

        • 感谢您的所有回答。它们都是有效的,但由于我只能将一个标记为解决方案,因此我选择它是因为它具有更高的教育价值:p。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多