-
template trouble
I'm working on a little class that runs marginally faster than vector< vector<> >.
The compiler doesn't like my syntax for specifying a type defined within the class template.
You'll get the error, then the code, then the class.
Code:
/home/david/Documents/c++/bitmap/bitmap/array2d.hpp|46|error: expected unqualified-id before ‘&’ token|
/home/david/Documents/c++/bitmap/bitmap/array2d.hpp|46|error: expected ‘,’ or ‘...’ before ‘&’ token|
/home/david/Documents/c++/bitmap/bitmap/array2d.hpp||In function ‘void for_each(array2d<T, uint>&)’:|
/home/david/Documents/c++/bitmap/bitmap/array2d.hpp|49|error: ‘top_left_point’ was not declared in this scope|
/home/david/Documents/c++/bitmap/bitmap/array2d.hpp|49|error: ‘bottom_right_point’ was not declared in this scope|
Code:
template<typename T, typename uint, class ternary_func_t>
inline void for_each(array2d<T,uint> & a, const array2d<T,uint>::point2d & top_left_point,
const array2d<T,uint>::point2d & bottom_right_point, ternary_func_t & f)
{
for(uint i = top_left_point.first; i < bottom_right_point.first; ++i)
{
for(uint j = top_left_point.second; j < bottom_right_point.second; ++j)
f( a, i, j );
}
}
Code:
template<typename T, typename uint = std::size_t>
class array2d
{
T** data;
const uint sz;
const uint w;
const uint h;
array2d(array2d<T> & other); //I want to disable
array2d(const array2d<T> & other); //copy behavior for now.
array2d & operator = (array2d<T> & other);
array2d & operator = (const array2d<T> & other);
public:
typedef std::pair<uint, uint> point2d;
array2d(uint, uint);
~array2d();
const uint size() const { return sz; }
const uint width() const { return w; }
const uint height() const { return h; }
T & operator()(uint x, uint y) { return data[x][y]; }
T & operator()(point2d p) { return (*this)(p.first, p.second); }
};
Thanks.
-
It would be better if you provided the smallest and simplest program that demonstrates the error. My guess is that array2d<T,uint>::point2d should be typename array2d<T,uint>::point2d.
-
That did it.
Thanks, and sorry for the dump.
-
No problem :)
By the way, to disable copying you just need to make these private:
Code:
array2d(const array2d<T> & other);
array2d & operator = (const array2d<T> & other);
The versions that have a non-const parameter are not const correct anyway.
-
There's Boost.Multi_Array, if you just need a multi-dimensional array.