2d array from boost::multi_array - unable to compile
I am trying to create a 2d array class based on boost::multi_array
. I face two issues in the code given below. (1) The code for the member function col()
does not compile saying that ::type’ has not been declared
. Where am I going wrong? (2) Is it possible to define the member function data()
outside the class? My attempt gives compile error as the typedefs are not available. But I am unable to define the typedefs outside the class because the typedefs in turn require the type T
which is available only inside the template class. Thanks.
#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?
A开发者_如何学编程rray2d(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]);
}
array_type::array_view<1>::type
You need template and typename here :)
typename array_type::template array_view<1>::type
^^^^^^^^ ^^^^^^^^
The keyword template is required because otherwise the < and > will be treated as less and greater because the array_type is a dependent name, and therefore whether array_view is a nested template or not is not known until instantiation.
(1) The code for the member function col() does not compile saying that ::type’ has not been declared.
array_type
is a dependant type on T
and array_type::array_view<1>::type
is still dependant on T
, you need a typename
.
(2) Is it possible to define the member function data() outside the class?
It sure is, but it shouldn't be a problem for it to be defined inside the class.
template< typename T >
typename Array2d< T >::element* Array2d< T >::data(){ ... }
精彩评论