2

STLリストを作成しています。MyList特別なクラス ( ) のリストであるデコレータ クラス ( ) を作成しましたProtectMe。リストのすべての項目を const にしたい。だからここに私が作ったものがあります:

#include <list>

using namespace std;

class ProtectMe{
private:
    int data_;
public:
    ProtectMe(int data):data_(data){
    }

    int data() const{return data_;}
};

class MyList{
private:
    //A list of constant pointers to constant ProtectMes.
    list<const ProtectMe* const> guts_;
public:
    void add(const ProtectMe& data){
        guts_.push_front(&data);
    }
};

次のコンパイル エラーが発生します。

エラー: 'const _Tp* __gnu_cxx::new_allocator::address(const _Tp&) const [with _Tp = const ProtectMe* const]' はオーバーロードできません

どこが間違っていたのかを解読しようとして、まだ頭を悩ませています。このコードがコンパイルされないのはなぜですか? 何を変更すればよいですか?

4

1 に答える 1

2

が機能するvalue_typeには、標準コンテナの がCopyInsertable(またはMoveInsertable) である必要がありますpush_front。の値の型list<const ProtectMe* const>は定数なので、ではありませんCopyInsertable

CopyInsertableは、

allocator_traits<A>::construct(m, p, v);

は明確に定義されてpおり、 は へのポインターvalue_typeです。これは、デフォルトで p の配置 new を呼び出すため、非 const ポインターである必要があります。

于 2012-07-06T20:14:44.627 に答える