Click to See Complete Forum and Search --> : problem w/ member array (and some advice plz)


Latem
February 17th, 2004, 07:22 PM
hello,

I have a classA, that is described in classA.h file, and implemented in classA.cpp file.
My classA needs to have an array of integers. the size and the contents of the array is known at compile time.

In my header file I have:

#define NITMES 8 // right at the start of file
... // declaration of member funcions, etc
int* items;
...

and in my constructor in .cpp file:

...
int initArray[NITEMS] = {3, 3, 4, 5, 6, 6, 9, 7};
items = initArray;
...

My problem is that items never gets assignmed to that array or something.

If I output items[0], items[1].. all I get is garbage.

since i am working in a vc++ app (MFC) with files, I have a CFile object (destFile) available, and I use it to write stuff to it for debugging purpoces as well.

so I also have in my code:

...
for(int m = 0; m< NITEMS; m++)
{
char conint[10];
itoa(items[m], conint, 10);
destFile->Write(conint, 10);
destFile->Write(" ", 2);
}
...

and it writes to a file the followeing garbage:

-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 124226860 -858993460

so that is my problem, why isn't items working right?

I also have a couble of general questions.

Whats the most "correct" way of implementing an array as a member of a class. I thought what I was doing was good, and I think I've seen it done before, but I would like some expert opinion.

As well I would like to ask whats the "correct" way to print things like this out when you are debugging a visual application. Like is there an easy way to print to standard output, or is printing to a file like this fine (since I am already working w/ files).

Sorry if I've put this in the wrong forum (instead of VC++), but I thought it was more of a general c++ issue.

Thanks for your time and help,

Latem

Paul McKenzie
February 17th, 2004, 07:59 PM
int initArray[NITEMS] = {3, 3, 4, 5, 6, 6, 9, 7};
items = initArray;
initArray is a local variable. When you assign the pointer, initArray goes out of scope (is invalid) when the constructor is completed. That's why you are getting the uninitialized values.

Why don't you just have the array member instead of a pointer? You're overcomplicating the code by introducing a pointer when you don't have to.

Regards,

Paul McKenzie

Latem
February 18th, 2004, 07:02 AM
thanks for reply, that makes all the sense. I don't know what I was thinking.

Why don't you just have the array member instead of a pointer? You're overcomplicating the code by introducing a pointer when you don't have to.

I tried that in the first place.

Instead of int* items, I declared an array int items[NITEMS];

and in my constructor if I do:

items = {3, 3, 4, 5, 6, 6, 9, 7};

the compiler throws a syntax error in the constructor.
however if I do the following:

items[0] = 3;
items[1] = 3;
...

everyting works fine, and the array is properly initialised. However I wouldn't really prefer this since I may end up having 50+ items later on, and I would hate writing it all out like that. :)

and if I just do everything in my header file like this:

int items[NITEMS] = {3, 3, 4, 5, 6, 6, 9, 7};

the compiler complains again. I don't remember the exact error, but may have been something along the lines "items is an undeclared identifier".

so what am I doing wrong?

thx,

Latem

Paul McKenzie
February 18th, 2004, 07:19 AM
One solution is to assign each element from the local array to your member array using a loop:

class foo
{
private:
int nItems[8];

public:
foo()
{
int Items[] = {3,3,5,1,2,3,5,6};
for ( int i = 0; i < 8; ++i }
nItems[ i ] = Items[ i ];
}
};

Regards,

Paul McKenzie

Latem
February 18th, 2004, 07:46 AM
that would work.

Thanks,

Latem