Help with type conversion error
When I try to compile my program, I get the following error:
main.cpp: In function ‘int main()’:
main.cpp:67: error: cannot convert ‘int (*)[(((long unsigned int)(((long int)mapSizeY) - 1)) + 1u)]’ to ‘int (*)[10]’ for argument ‘3’ to ‘void initializeMap(int, int, int (*)[10])’
main.cpp:68: error: cannot convert ‘int (*)[(((long unsigned int)(((long int)mapSizeY) - 1)) + 1u)]’ to ‘int (*)[10]’ for argument ‘3’ to ‘void paintMap(int, int, int (*)[10])’
My code looks like this:
#include <iostream>
using namespace std;
void initializeMap(int mapSizeX, int mapSizeY, int map[][10])
{
// Map details:
// 0 = # (wall)
// 1 = space (free space)
// 2 = x (player)
for(int x = 0; x < mapSizeX; x++)
{
map[x][0] = 0;
}
for(int y = 0; y < (mapSizeY - 2); y++)
{
map[0][y] = 0;
for(int x = 0; x < (mapSizeX - 2); x++)
{
map[x][y] = 1;
}
map[mapSizeX][y] = 0;
}
for(int x = 0; x < mapSizeX; x++)
{
map[x][mapSizeY - 1] = 0;
}
}
void paintMap(int mapSizeX, int mapSizeY, int map[][10])
{
for(int y = 0; y < mapSizeY; y++)
开发者_JAVA技巧 {
for(int x = 0; x < mapSizeX; x++)
{
switch(map[x][y])
{
case 0:
cout << "#";
break;
case 1:
cout << " ";
break;
case 2:
cout << "x";
break;
}
cout << map[x][y];
}
cout << endl;
}
}
int main()
{
int mapSizeX = 10;
int mapSizeY = 10;
int map[mapSizeX][mapSizeY];
initializeMap(mapSizeX, mapSizeY, map);
paintMap(mapSizeX, mapSizeY, map);
cout << endl << endl;
return 0;
}
I've spent an hour trying to solve the issue and about twenty minutes searching for a solution. Can any of you help me out?
C++ does not support variable-length arrays, which is what map
is in your code. However, some compilers may support it as a non-standard extension. However, it certainly won't be compatible with a function expecting a "standard" array.
If you make mapSizeX
and mapSizeY
constants, this should work.
Declare mapSizeX
and mapSizeY
const
. Your current code, as is, is basically not wellformed according the C++ language specification which allows only constants as array size specifiers.
The error message is utterly misleading, that's due to the fact that some compiler support this as an extension and the latest C language standard includes it as well.
I tested it here.
Your function calls expect the second dimension to be exactly 10, always. Your code has that dimension in a variable (mapSizeY
), which is not guaranteed to be 10, even though you set it a line earlier.
Change mapSizeY
to const int
so the compiler can optimize it away.
If you make mapSize[X|Y]
const in the main method, this should work. Alternatively, as you're passing the dimensions to each of your methods, why not pass the matrix as an int**
?
The problem is the variable-length automatic array. One way to fix this is to make the item look the same in main() as it does in initializeMap() and paintMap().
int main()
{
int mapSizeX = 10;
// int mapSizeY = 10;
int map[mapSizeX][10];
initializeMap(mapSizeX, 10, map);
paintMap(mapSizeX, 10, map);
cout << endl << endl;
return 0;
}
One thing I don't suggest is simply declaring MapSize? to be const. Then it looks like the array is variable-length when it really isn't.
You should also be aware that variable-length automatic arrays are a GNU extension to C89 and C++.
Change the definition map[][10]
to map[][]
or *map
in the initializeMap an paintMap functions.
精彩评论