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
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