template trouble

This is a discussion on template trouble within the C++ Programming forums, part of the General Programming Boards category; I'm working on a little class that runs marginally faster than vector< vector<> >. The compiler doesn't like my syntax ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907

    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.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    That did it.

    Thanks, and sorry for the dump.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There's Boost.Multi_Array, if you just need a multi-dimensional array.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Template Trouble - Brainache Please Help
    By SpaceCadet in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2006, 08:51 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21