473,387 Members | 1,579 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.

class with two dimensional array as a member (I need help)

Hello!

I'd like to have a two dimensional array as a member of a class, bu if
I did it in the following way:

class A {
const int n;
int a[n][n];
public:
A(int nn): n(nn) {};
~A() {};
};

this produce error, when I changed it to:

class A {
const int n;
int** a;
public:
A(int nn): n(nn) {
for (int i=0;i<n;i++)
a[i]=new int[n];
}
~A() {
for (int i=0;i<n;i++)
delete [] a[i];
}
};

it works fine, however when I tried to use it in some program like
this:

int main() {
int x,y;
A a(10);
return 0;
}

program compiled without any problems, but when I run it it was thrown
an unknown exception ... I don't understand what is going on.
Jun 27 '08 #1
1 1086
On May 13, 9:18 am, "Alf P. Steinbach" <al...@start.nowrote:
* Pawel_Iks:
Just a couple of nits (and the correction of a typo), but...
<code>
class A
{
private:
size_t myN;
std::vector<int myElements;

size_t indexFor( size_t i, size_t j ) const
{
return myN*i + j;
}
public:
A( size_t n ): myN( n ), myElements( n*n ) {}
int at( size_t i, size_t j ) const
{
return myElements.at( indexFor( i + j ) );
You certainly meant "indexFor( i, j )". As written, it won't
compile (and wouldn't do the right thing if it did).

Also, I very much question the wisdom of using at() here. If
there's an error in the indexing, you want an assertion failure,
not an exception. Which is what you'll get with any reasonable
implementation of std::vector<>::operator[]. But of course,
even that is only partially right, since something like 20, 0
will pass even if myN is 10. You really need to use assert in
indexFor, i.e.:

size_t indexFor( size_t i, size_t j ) const
{
assert( i < myN && j < myN ) ;
return myN*i + j;
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
3
by: josephweiss | last post by:
Here's my quandry my bundles table ID bottleTotal price ========================== 1 1 10.00 2 6 8.00 3 12 7.50
2
by: DevarajA | last post by:
Can someone help me understand what flexible array members exactly are, how they behave and how could them be implemented by a i386? Also I didn't understand the two exceptions that the standards...
3
by: Bryan Ruddy via .NET 247 | last post by:
I need to interate through a DB and fill an array. I need to do this with a multi dimensional array. I need some sample code on how to accomplish this. -------------------------------- From:...
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
15
by: Eric Lilja | last post by:
Hello, in one of my classes I have this member variable: Tile ***tiles_; where Tile is another one of my classes. dimensional array of Tile-pointers (dimensions are of equal sizes). Last, I...
4
by: Gernot Frisch | last post by:
Hi, I need a class, that has a 4 dimensional array (can be 3 dimensional, too) with such an operator: T operator()(int x1, int x2=0, int x3=0, int x4=0); that can be used as:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.