Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?
    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]);
}
share|improve this question

2 Answers 2

up vote 2 down vote accepted
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.

share|improve this answer
    
I had figured out with the help of compiler error message and the input from K-ballo. But why the template requirement? –  suresh Sep 26 '11 at 18:33
    
@suresh: See my edit –  Armen Tsirunyan Sep 26 '11 at 18:34
1  
@suresh : For more details, see this FAQ: What is the ->template, .template and ::template syntax about? –  ildjarn Sep 26 '11 at 18:35

(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(){ ... }
share|improve this answer
    
Thanks. I modified the problem line as typename array_type::template array_view<1>::type myview = array[boost::indices[range()][x]]; and it compiled. But then, why this explicit mention of template is required? The compiler suggested it though. (gcc 4.4.3) –  suresh Sep 26 '11 at 18:30
1  
@suresh: Because is a dependent template; dependant types need typename and dependent templates need template keywords. –  K-ballo Sep 26 '11 at 18:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.