Dynamic 2D array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vmohanaraj
    New Member
    • Jul 2006
    • 14

    Dynamic 2D array

    Hi,

    I have implemented a template class which encaptulates a 2d dynamic array. Basically the class looks like this:


    template <class T>
    class Dynamic2dArray {

    private:
    std::vector<std ::vector<T> > data;

    public:
    Dynamic2dArray( );

    Dynamic2dArray( int rows);
    Dynamic2dArray( int row, int col);
    ............... ...............
    ............... ............... ..

    std::vector<std ::vector<T> >::const_iterat or begin() const;
    };

    template <class T>
    std::vector<std ::vector<T> >::const_iterat or Dynamic2dArray< T>::begin() const{
    return data.begin();
    }


    I have implemented everything except this last method: begin() which is supposed to return the iterator pointing the first element in the vector in the same manner as used in std::vector.

    I am getting the following compilation error pointing to the declaration of this end method: error: expected `;' before "begin".

    I also noticed that when I try to declare a variable itRow (shown below) in another instance the same error is raised.

    std::vector<std ::vector<T> >::const_iterat or itRow;


    Does any one know how to solve this problem? I would really appreciate your prompt response.

    Thanks and Regards,
    Mohan.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Must be something you've not posted, the code you have posted compiles for me without error. Perhaps you have a mismatched { or }

    BTW a common mistake that is made is that the internal data in a class has to match the external interface. By this I mean that you are implementing a 2D dynamic array so you have declared your class internally with a 2D array using vectors. Now sometimes this may be the sensible way of doing it (I can not judge for your case) but it can also make sense to have a 1D array of data internally and calculate your offsets into it.

    Comment

    • vmohanaraj
      New Member
      • Jul 2006
      • 14

      #3
      Hi Banfa,

      Thanks for your reply. I just can not believe the behaviour of this code that even when I compile only the part of the code that I sent you(with empty implementation of the constructors added), I get the same error. Could you explain me what you mean by "mismatched {or}". BTW I am using Code::Block IDE with compiler GNU GCC version 3.4.4.

      Yes. I agree with your suggestion. I really thought about implementing 1D vector inside. But in my case, the number of rows for each column varies immensely in size. For example, the first row might have 1 element while the second has 500 elements. So in order to save the space, I decided in this way.
      In the code, I have made sure that only the required amount of space is allocated, not more. I thought that If we want to have different number of rows as in my case, the offset technique won't work. Am I correct in that?

      Thanks and Regards,
      Mohan.

      Comment

      • vmohanaraj
        New Member
        • Jul 2006
        • 14

        #4
        It just clicked into my mind what you mean by { or }. But they are not mismatched. I checked them. Still I am getting the same error.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          I have to say I am a little bit stumped by this one, I wonder if it's a sneaky ANSI compliance thing, I can't switch on ANSI compilance beacuse I am use MSVC++ and they have not written their C++ headers so they compile under ANSI compliance.

          Comment

          • Prasannaa
            New Member
            • May 2007
            • 21

            #6
            Hi,
            you have to prefix std::vector<std ::vector<T> >::const_iterat or with
            typename as
            Code:
            typename   std::vector<std::vector<T> >::const_iterator
            on both your declaration and definition.

            What would be more elegant is to typedef the const_iterator as

            Code:
             typedef typename std::vector<std::vector<T> >::const_iterator  const_iterator;
            and then start using them in places.This also allows the clinets to write code
            like Dynamic2dArray: :const_iterator itr = darray.begin();

            Regards
            Prasannaa

            Comment

            Working...