0

に基づいて 2 次元配列クラスを作成しようとしていますboost::multi_array。以下のコードで 2 つの問題に直面しています。(1) メンバー関数のコードcol()はコンパイルされません::type’ has not been declared。どこが間違っていますか?data()(2)クラス外でメンバー関数を定義することは可能ですか? typedef を使用できないため、コンパイル エラーが発生します。Tしかし、typedefはテンプレート クラス内でのみ使用できる型を必要とするため、クラス外で typedef を定義することはできません。ありがとう。

#include <boost/multi_array.hpp>
#include <algorithm>

template <class T>
class Array2d{
public:
    typedef typename boost::multi_array<T,2> array_type;
    typedef typename array_type::element element;
    typedef boost::multi_array_types::index_range range;

    //is it possible to define this function outside the class?
    Array2d(uint rows, uint cols);
    element * data(){return array.data();}

    //this function does not compile
    template<class Itr>
    void col(int x, Itr itr){
        //copies column x to the given container - the line below DOES NOT COMPILE
         array_type::array_view<1>::type myview  = array[boost::indices[range()][x]];
         std::copy(myview.begin(),myview.end(),itr);
    }

private:
    array_type array;

    uint rows;
    uint cols;
};

template <class T>
Array2d<T>::Array2d(uint _rows, uint _cols):rows(_rows),cols(_cols){
    array.resize(boost::extents[rows][cols]);
}
4

2 に答える 2

2
array_type::array_view<1>::type

ここにテンプレートとタイプ名が必要です:)

typename array_type::template array_view<1>::type
^^^^^^^^             ^^^^^^^^

キーワード template が必要です。そうしないと、array_type が従属名であり、array_view がネストされたテンプレートであるかどうかがインスタンス化されるまでわからないため、< と > はより少ないまたはより大きなものとして扱われます。

于 2011-09-26T18:32:14.763 に答える
1

(1) メンバー関数 col() のコードは、::type' が宣言されていないため、コンパイルされません。

array_typeは に依存するタイプでTあり、array_type::array_view<1>::typeまだ依存しているT場合は、 が必要ですtypename

(2) メンバー関数 data() をクラス外で定義することは可能ですか?

確かにそうですが、クラス内で定義しても問題ありません。

 template< typename T >
 typename Array2d< T >::element* Array2d< T >::data(){ ... }
于 2011-09-26T18:17:46.787 に答える