473,320 Members | 1,946 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,320 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 6016
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.