2011-09-26 53 views
0

我想根据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]); 
} 

回答

2
array_type::array_view<1>::type 

你需要模板类型名称在这里:)

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

关键字模板是必需的,因为否则<而且由于ARRAY_TYPE是一个从属名称>将被视为越来越更大,因此在实例化之前,array_view是否为嵌套模板是未知的。

+0

我已经想出了编译器错误信息和K-ballo输入的帮助。但为什么'模板'的要求? – suresh

+0

@suresh:查看我的编辑 –

+1

@suresh:有关更多详细信息,请参阅此FAQ:[模板,'.template'和':: template'语法是什么?](http:// www。 comeaucomputing.com/techtalk/templates/#templateprefix) – ildjarn

1

(1)的成员函数COL的代码()不编译说::类型”尚未声明。

array_typeT一个依赖型和array_type::array_view<1>::type仍然依赖T,你需要一个typename

(2)是否可以在类之外定义成员函数data()?

它确实是,但它不应该是一个问题,它不应该是在类内定义的问题。

template< typename T > 
typename Array2d<T>::element* Array2d<T>::data(){ ... } 
+0

谢谢。我将问题行修改为'typename array_type :: template array_view <1> :: type myview = array [boost :: indices [range()] [x]];'并编译它。但是,那么为什么明确提到'template'是必需的?编译器提示它。 (gcc 4.4.3) – suresh

+1

@suresh:因为是依赖模板;依赖类型需要'typename',依赖模板需要'template'关键字。 –