2011-09-26 3 views
0

boost::multi_array 기반으로 2 차원 배열 클래스를 만들려고합니다. 나는 아래에 주어진 코드에서 두 가지 이슈에 직면 해있다. (1) 멤버 함수 col()에 대한 코드는 컴파일하지 않습니다. ::type’ has not been declared. 내가 어디로 잘못 가고 있니? (2) 클래스 외부에서 멤버 함수 data()을 정의 할 수 있습니까? 내 시도 typedefs 사용할 수 없으므로 컴파일 오류를 제공합니다. 하지만 typedef는 템플릿 클래스 내에서만 사용할 수있는 T 유형을 필요로하므로 클래스 외부의 typedef를 정의 할 수 없습니다. 감사.2 차원 배열에서 boost :: multi_array - 컴파일 할 수 없습니다.

#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의 입력을 통해 알아 냈습니다. 그러나 왜'template' 요구 사항입니까? – suresh

+0

@suresh : 내 편집보기 –

+1

@suresh : 자세한 내용은 다음 FAQ를 참조하십시오. [[-> template','.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 :: index [range()] [x]];와 같이 수정하고 컴파일했다. 그렇다면 왜이 '템플릿'에 대한 명시적인 언급이 필요한가? 컴파일러는 그것을 제안했다. (gcc 4.4.3) – suresh

+1

@suresh : 종속 템플릿입니다. 의존형은'typename'을 필요로하고 의존성 템플릿은'template' 키워드를 필요로합니다. –

관련 문제