2

カスタム コンテナーを使用しようとしていますが、そのコンテナーのコンストラクターでメモリ プール アロケーターを渡します。全体は次のように始まります。

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 フレームワーク

私はこれで私の頭を超えたと思います....

4

3 に答える 3

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 &)これは、1 つの引数、への参照を受け取るコンストラクタです。IAllocator
  • cannot convert parameter 1コンパイラが最初の(そして唯一の)引数の型に問題があることを意味します
    • あなたはそれにこのタイプを与えました:... from 'Allocators::IAllocator *'
    • そして、このタイプが必要でした(コンストラクター宣言と一致するため):... to 'Allocators::IAllocator &'

では、持っているポインターからコンストラクターが必要とする参照にどのように変換しますか?


OK、実際の回答も追加しました:

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);
于 2012-05-02T13:19:21.237 に答える
1

make_new参照ではなくポインタで呼び出しているようです。試す:

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

そして、インデントスタイルを選んでそれに固執してください.

于 2012-05-02T13:08:02.500 に答える
0

アロケータファクトリはアロケータへのポインタを返していますが、コンストラクタはアロケータへの参照を必要としています。ポインタを逆参照する必要があります。

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); 
于 2012-05-02T13:08:43.670 に答える