473,387 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_iterator begin() const;
};

template <class T>
std::vector<std::vector<T> >::const_iterator 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_iterator itRow;


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

Thanks and Regards,
Mohan.
Aug 1 '06 #1
5 6019
Banfa
9,065 Expert Mod 8TB
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.
Aug 1 '06 #2
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.
Aug 1 '06 #3
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.
Aug 1 '06 #4
Banfa
9,065 Expert Mod 8TB
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.
Aug 1 '06 #5
Hi,
you have to prefix std::vector<std::vector<T> >::const_iterator with
typename as
Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1.  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
May 4 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
0
by: pjr | last post by:
Using VS2005, I dynamically create an event delegate. Code follows question. My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
13
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.