I'm trying to return a pointer to a 2d char array (mainly just for practice using pointers since I don't understand them too well yet).
我正在尝试返回一个指向2d char数组的指针(主要是为了练习使用指针,因为我还不太了解它们)。
When I compile this I get the message:
当我编译这个时,我收到消息:
Maze Game.cpp(32): error C2440: 'initializing' : cannot convert from 'char (*)[8]' to 'char **'
Line 32 is:
第32行是:
char** acBoard = new char[8][8];
Here is the source code:
这是源代码:
#include "stdafx.h"
#include <iostream>
char** createGrid();
int main()
{
using namespace std;
char** pBoard = createGrid();
char gameBoard[8][8];
for(int row = 0; row < 8; row++) {
int count = 0;
for(int col = 0; col < 8; col++) {
char temp = **pBoard + count;
gameBoard[row][col] = temp;
cout << gameBoard[row][col];
}
cout << endl;
}
delete pBoard;
pBoard = 0;
return 0;
}
char** createGrid()
{
char** acBoard = new char[8][8];
//Set wall positions
acBoard[1][6] = 'X';
acBoard[1][7] = 'X';
acBoard[3][4] = 'X';
acBoard[3][6] = 'X';
acBoard[3][8] = 'X';
acBoard[4][1] = 'X';
acBoard[4][3] = 'X';
acBoard[4][4] = 'X';
acBoard[4][5] = 'X';
acBoard[4][6] = 'X';
acBoard[4][7] = 'X';
acBoard[5][1] = 'X';
acBoard[5][3] = 'X';
acBoard[5][4] = 'X';
acBoard[5][8] = 'X';
acBoard[6][1] = 'X';
acBoard[6][2] = 'X';
acBoard[6][3] = 'X';
acBoard[6][6] = 'X';
acBoard[6][8] = 'X';
acBoard[7][1] = 'X';
acBoard[7][5] = 'X';
acBoard[7][6] = 'X';
acBoard[7][8] = 'X';
acBoard[8][3] = 'X';
acBoard[8][5] = 'X';
acBoard[8][6] = 'X';
acBoard[8][7] = 'X';
acBoard[8][8] = 'X';
acBoard[1][8] = 'N';
acBoard[7][7] = 'T';
acBoard[5][2] = 'W';
acBoard[2][2] = '$';
return acBoard;
}
Can anyone explain to me why this is happening?
任何人都可以向我解释为什么会这样吗?
2 个解决方案
#1
1
See link regards allocating a 2D array using new.
请参阅使用new分配2D数组的链接。
It's much simpler to understand if you do this:
如果你这样做,理解起来要简单得多:
struct Board
{
char cells[8][8] ;
};
Board* create()
{
Board* board = new Board;
return board;
}
And of course as soon as we get that far, we realise this is C++, change struct
to class
and make it a proper object.
当然,只要我们到目前为止,我们就会意识到这是C ++,将struct更改为class并使其成为一个合适的对象。
#2
1
It's telling you exactly what the problem is - char **
and your 2D array aren't compatible types. You need:
它告诉你究竟是什么问题 - char **和你的2D阵列不兼容类型。你需要:
char (*acBoard)[8] = new char[8][8];
And corresponding changes elsewhere. Alternately, you could change your allocation to create an array of pointers to other arrays.
其他地方相应的变化。或者,您可以更改分配以创建指向其他数组的指针数组。
Aside: C++ uses 0-indexed arrays.
旁白:C ++使用0索引数组。
#1
1
See link regards allocating a 2D array using new.
请参阅使用new分配2D数组的链接。
It's much simpler to understand if you do this:
如果你这样做,理解起来要简单得多:
struct Board
{
char cells[8][8] ;
};
Board* create()
{
Board* board = new Board;
return board;
}
And of course as soon as we get that far, we realise this is C++, change struct
to class
and make it a proper object.
当然,只要我们到目前为止,我们就会意识到这是C ++,将struct更改为class并使其成为一个合适的对象。
#2
1
It's telling you exactly what the problem is - char **
and your 2D array aren't compatible types. You need:
它告诉你究竟是什么问题 - char **和你的2D阵列不兼容类型。你需要:
char (*acBoard)[8] = new char[8][8];
And corresponding changes elsewhere. Alternately, you could change your allocation to create an array of pointers to other arrays.
其他地方相应的变化。或者,您可以更改分配以创建指向其他数组的指针数组。
Aside: C++ uses 0-indexed arrays.
旁白:C ++使用0索引数组。