-
May 19th, 2005, 04:50 PM
#1
array declaration question
my.cpp file:
Code:
#define size 55
int array1[size];
int *array2[size];
size may vary.
if i add these to my.h file as:
Code:
int array1[];
int *array2[];
i get error: an illegal zero-sized array.
how do i handle this?.
-
May 19th, 2005, 04:53 PM
#2
Re: array declaration question
Originally Posted by %chess%
how do i handle this?.
Like this, for example:
Code:
int* array1;
int** array2;
But since you are using C++: Why bother about C-style arrays at all, and not std::vector instead?
Besides that: What's your intention behind declaring a variable in a header file? That's usually not a good idea.
-
May 19th, 2005, 04:54 PM
#3
Re: array declaration question
If your goal is to have a resizable array, use the std::vector class that has been discussed here many times.
Regards,
Paul McKenzie
-
May 19th, 2005, 05:44 PM
#4
Re: array declaration question
int *databuf[arraysize];
for (int i = 0; i < 10; i++)
databuf[i] = new int[sizeofdata];
do i do the above with vectors?.
-
May 19th, 2005, 05:58 PM
#5
Re: array declaration question
You can do the equivalent by constructing a vector of vectors of ints using the constructors that take the initial size of the vector:
Code:
std::vector<std::vector<int> > databuf(arraysize, std::vector<int>(sizeofdata));
You can also use typedefs to make the code smaller if the look of it bothers you.
-
May 19th, 2005, 06:45 PM
#6
Re: array declaration question
std::vector<std::vector<int> > databuf(m_arraysize, std::vector<int>(m_sizeofdata));
How do i access this globally within an object from different functions?
i was getting an error when added it to public member of
CMy class. need to push to its stack area from one function and
pop from its stack area from another function.
unknown m_arraysize.it needs to know m_arraysize?. maybe i cam confused.
could you illustrate more?.
-
May 19th, 2005, 07:01 PM
#7
Re: array declaration question
As part of a class, you declare it in the class and initialize it in the constructor(s)' initializer list(s):
Code:
class Example
{
public:
Example(int arraysize, int sizeofdata) : databuf(arraysize, std::vector<int>(sizeofdata))
{ }
int getArraySize() const { return databuf.size(); }
int getSizeOfData(int index = 0) const { return databuf.at(index).size(); }
private:
std::vector<std::vector<int> > databuf;
}
If you are still unsure, you might want to post more of your code so we know what exactly you are having trouble with. There are more ways to initialize your vectors than the way I used above, but hopefully you will be able to figure out how to get the sizes to the constructor on your own.
-
May 19th, 2005, 08:28 PM
#8
Re: array declaration question
sorry, One last thing since i am starting to look at
vectors more seriously than just a c++ extra thing.
How do i pass each databuf buffer to PutDataInBuffer function
to put data into it?. that was my intention. to allocate space
and pass it on.
Code:
for (int i = 0; i < size; i++)
{
BOOL (PutDataInBuffer(dataBuffers[i], Status))
{
//
}
}
-
May 19th, 2005, 09:31 PM
#9
Re: array declaration question
I guess this what should pass databuf[idx].at(idx)....???
should work...
-
May 20th, 2005, 12:29 AM
#10
Re: array declaration question
Originally Posted by %chess%
int *databuf[arraysize];
for (int i = 0; i < 10; i++)
databuf[i] = new int[sizeofdata];
do i do the above with vectors?.
Take a look at the following introduction to the 'vector' class...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|