Hi, I'm working on my first program in C++ (switching from Java) and I'm having a problem with a 2D array and pointers.
I have 4 arrays of data structures (structs) and I need to merge/concatenate them into one large array. I'm trying to do this by filling an array with the 4 arrays I need to merge, then using for loops to combine them. Below is my code (the method "importData()" returns data-type "record*", where "record" is my struct; and "fileNames[]" is an array contained the file names of the data files I'm importing from):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int a,b,c,d,blockSize;
record dataBlocks[4][1];
for(int n = 0; n < 4; n++)
{
dataBlocks[n][0] = importData(fileNames[n]);
}
blockSize = sizeof(dataBlocks[0][0])/sizeof(record);
data = new record[4*blockSize];
for(a = 0; a < blockSize; a++)
data[a] = dataBlocks[0][a];
for(b = 0; b < blockSize; b++)
data[a+b] = dataBlocks[1][b];
for(c = 0; c < blockSize; c++)
data[a+b+c] = dataBlocks[2][c];
for(d = 0; d < blockSize; d++)
data[a+b+c+d] = dataBlocks[3][d];
This is the error I'm getting:
line 5: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'record *' (or there is no acceptable conversion)
Is there any particular reason you want to do this using arrays? If not, I'd suggest vectors.
I believe you'll find them very useful in doing that sort of stuff.
The second array dimension on your line 2: record dataBlocks[4][1];
Is that a 1?
You need at least 2 elements to form an array. I'm guessing the compiler interprets the above as equivalent to: record dataBlocks[4];
Which might make dataBlocks[n][0] a record*
Or...
The function importData() returns a record*, which is being assigned to a record. Has this approach using 4x1 arrays worked before?
As I said, I'm completely new to C++, and this is my very first program in C++. So I'm still trying to get a hang on pointers and memory references. My thought process was that I could have an array of pointers (of type 'record') that pointed to arrays (of type 'record'). So I guess I don't really need a 2D array, but I was getting the same error when I tried using a 1D array.I guess my logic is flawed somewhere.
Looks like I'll be using vectors because they are much easier to work with. But out of curiosity, why doesn't my code work?
My thought process was that I could have an array of pointers (of type 'record') that pointed to arrays (of type 'record'). [...] But out of curiosity, why doesn't my code work?
Your logic is correct, you just had some trouble implementing it. If you want to use arrays, the solution should look like this:
The good things with vectors are that you don't have to do the cleanup manually, your 'small' containers may vary in size independently, and the number and size of your 'small' containers may be determined in run-time
(you could still pull the last two off using arrays, but it would be trickier and more verbose).
PS: Just in case you didn't notice yet, there's a very good tutorial in this site that could help a lot:
Wow, I did not know that an array can have only 1 or even 0 elements!
Mmmm... After reading your post in the lounge, I thought that your first post here was a bluff to test the OP.
I guess it wasn't... Or maybe it was, and this here is a second bluff to support the first one xD
I guess you're the only one who knows for real :P
My posts were sincere.
I have never thought to declare an array with fewer than 2 elements, since that seems non-sensical. However, A[0] and A[1] make perfect sense in use (just the first 2 elements of A).