2

コンパイルできないコードについての簡単な質問です。std::vector のラッパーを書きました:

template <class T>
class CLArray
{
public:
    /// Constructor, destructor.
    CLArray( const size_t size );
    CLArray( const size_t size, const T value );
    ~CLArray();

    /// Copy constructor and copy assignment operator.
    CLArray( const CLArray& rhs );
    CLArray& operator=( const CLArray& rhs );

    /// Move constructor and move assignment operator.
    CLArray( CLArray&& rhs );
    CLArray& operator=( CLArray&& rhs );

    void swap( CLArray& other )
    {
        std::swap( data_, other.data_ );
    }

    typedef typename std::vector<T>::iterator iterator;
    typedef typename std::vector<T>::const_iterator const_iterator;

    iterator begin()
    {
        return data_.begin();
    }

    const_iterator begin() const
    {
        return data_.begin();
    }

    iterator end()
    {
        return data_.end();
    }

    const_iterator end() const
    {
        return data_.end();
    }

    T& operator[]( const size_t index ) throw(CLException);
    T  operator[]( const size_t index ) const throw(CLException);

    T At( const size_t index ) const throw(CLException);
    void SetAt( const size_t index, const T& value ) throw(CLException);

    void Insert( ubyte* data, const size_t size );

    size_t GetSize() const;

    const CLArray<T>& GetData() const;

    void Clear();

private:
    std::vector<T> data_;
};

2 次元の CLArray を管理するためのクラスを作成したいと考えています。

template <class T>
class SomeMap
{
public:
    SomeMap( const size_t width, const size_t heigth, const T defaultVal = 0 )
        : map_( width, CLArray<T>(heigth, defaultVal) )
    {
        // Allocate enough memory for all objects
    }

    ~SomeMap() {}

private:
    CLArray<CLArray<T>> map_;
    //std::vector<std::vector<T>> map_;
};

そして、私はエラーを受け取ります:no matching function for call to ‘CLArray<Cell*>::CLArray()オブジェクトSomeMap<Cell*> map( 748, 480, nullptr );を宣言するとき、私は本当に理由を理解していません... `

4

2 に答える 2

1

CLArrayデフォルトのコンストラクターはありません。ベクトルのいくつかのメソッドでは、T がデフォルトで構築可能である必要があります。

これをインスタンス化すると:

SomeMap<Cell*> map( 748, 480, nullptr );

SomeMap にはプライベート メンバーがあります。

CLArray<CLArray<T>> map_;

プライベート メンバーの場合、 CLArray ストアvector<T>、T は CLArray です。この make の SomeMap のメンバーは評価され、含まれるオブジェクトがデフォルトで構築可能であることを必要としますvector<CLArray<Case*>>。デフォルトのコンストラクタはありません。したがって、T をインスタンス化しようとする vector のコードの奥深くで、コンパイラ エラーが発生する可能性があります。vectorCLArray

CLArray には既定のコンストラクターが必要だと思うかもしれません。ただし、任意のコンストラクターを指定すると、C++ が既定で提供する既定のコンストラクターが失われます。(ここを参照)。

于 2013-09-16T13:38:54.000 に答える