0

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]);
}

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

2 Comments

I had figured out with the help of compiler error message and the input from K-ballo. But why the template requirement?
@suresh : For more details, see this FAQ: What is the ->template, .template and ::template syntax about?
1

(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(){ ... }

2 Comments

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: Because is a dependent template; dependant types need typename and dependent templates need template keywords.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.