我想根据boost::multi_array
创建一个2d数组类。我在下面给出的代码中面临两个问题。 (1)成员函数col()
的代码不会编译为::type’ has not been declared
。我哪里错了? (2)是否可以在课堂外定义成员函数data()
?由于typedefs不可用,因此我尝试给出编译错误。但是我无法在类之外定义类型定义,因为类型定义反过来需要类型T
,该类型仅在模板类中可用。谢谢。来自boost :: multi_array的2d数组 - 无法编译
#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]);
}
我已经想出了编译器错误信息和K-ballo输入的帮助。但为什么'模板'的要求? – suresh
@suresh:查看我的编辑 –
@suresh:有关更多详细信息,请参阅此FAQ:[模板,'.template'和':: template'语法是什么?](http:// www。 comeaucomputing.com/techtalk/templates/#templateprefix) – ildjarn