1

I'm having trouble in compiling the following code. The compiler states:

Assigning to 'RawChunk::Ptr' (aka 'unsigned char (*)[128][16]') from incompatible type 'RawChunk::Chunk *' (aka 'unsigned char (*)[16][128][16]');

Does anyone know what I'm doing wrong? It seems like a simple mistake but I'm not sure what I'm supposed to change.

template <int dim>
class RawChunkWindow {
public:
    typedef unsigned char (*Ptr)[ChunkDimensions::MAX_Y][ChunkDimensions::MAX_Z];
    typedef unsigned char Chunk[ChunkDimensions::MAX_X][ChunkDimensions::MAX_Y][ChunkDimensions::MAX_Z];
    RawChunkWindow() {
        for (int i = 0; i < dim; ++i) {
            for (int j = 0; j < dim; ++j) {
                window[i][j] = &payload[i][j]; //compiler complains here
            }
        }
    }
private:
    Ptr window[dim][dim];
    Chunk payload[dim][dim];
};
Xavier
  • 8,828
  • 13
  • 64
  • 98

3 Answers3

3

Without a further explanation of what you intend on doing, I cannot be really sure, but it seems that you might have used the incorrect types for the member variables. In particular, the member variable window is of type:

window: Array of size dim of array of size dim of pointers to array of MAX_Y arrays of MAX_Z unsigned chars, that is a 2 dimensional array of pointers to 2 dimensional arrays.

payload: Array of size dim of array of size dim of array of size MAX_X of array of size MAX_Y of array of size MAX_Z, that is, a 5 dimensional array.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
1

From your code snipped in the constructor of RawChunkWindow, it looks like your Ptr types should be pointers to Chunk types, but your typedef declarations don't reflect that. Instead Ptr is a pointer to a two-dimensional array of char types, not a pointer to a three-dimensional array.

So change this:

typedef unsigned char (*Ptr)[ChunkDimensions::MAX_Y][ChunkDimensions::MAX_Z];

to this:

typedef unsigned char (*Ptr)[ChunkDimensions::MAX_X][ChunkDimensions::MAX_Y][ChunkDimensions::MAX_Z];

This is also what your error code is reporting. A unsigned char (*)[128][16] is a pointer to a 128x16 two-dimensional array, and the compiler is complaining that you're attempting to assign a three-dimensional 16x128x16 array pointer rvalue (i.e., a unsigned char (*)[16][128][16] rvalue) to the wrong pointer-type.

As a side-node, be careful how you declare this object if you are planning on using them on the stack. A single Chunk type instance will take up 32Kb if the dimensions are 16x128x16, and a two-dimensional array of those could quickly eat up your stack-space if you pass too large a value as the template argument for dim.

Jason
  • 31,834
  • 7
  • 59
  • 78
0

To eliminate the error the way you currently have it, you need to change the line:

window[i][j] = &payload[i][j];

to

window[i][j] = &payload[i][j][x];

for some "x" less than "ChunkDimensions::MAX_X". It's not clear to me what you're trying to do however, and the above fix almost certainly isn't it (probably). In simple terms, it appears that you may be trying to store a collection of 3D arrays in one collection (payload), and since a 3D array is really just an array of 2D arrays, you're then trying to store a pointer to each of these 2D arrays in another collection (window). I won't even get into the use of "dim" here, since the situation is already enough to twist one's head into a knot. In any case, using a "std::vector" instead of raw c-style arrays would be much easier. For a better understanding of the issues using c-style arrays in general (in case you can't use a "std::vector" for some reason), see my previous responses to 2Dimensional Array Pointer manipulation in C++ and manipulating multidimensional arrays with functions in C++. You can extend the concepts I present to 3D arrays.

Community
  • 1
  • 1
Larry
  • 796
  • 6
  • 13