gpt4 book ai didi

c++ - 试图将一个点返回到一个二维字符数组

转载 作者:行者123 更新时间:2023-11-28 03:07:51 26 4
gpt4 key购买 nike

我正在尝试返回一个指向二维字符数组的指针(主要是为了练习使用指针,因为我还不太了解它们)。

当我编译它时,我得到消息:

Maze Game.cpp(32): error C2440: 'initializing' : cannot convert from 'char (*)[8]' to 'char **'

第 32 行是:

char** acBoard = new char[8][8];

这里是源代码:

#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;

}

谁能向我解释为什么会这样?

最佳答案

参见 link关于使用 new 分配二维数组。

如果你这样做,理解起来就简单多了:

struct Board
{
char cells[8][8] ;
};

Board* create()
{
Board* board = new Board;
return board;
}

当然,一旦我们走到这一步,我们就会意识到这是 C++,将 struct 更改为 class 并使其成为一个合适的对象。

关于c++ - 试图将一个点返回到一个二维字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19261004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com