【问题标题】:Store pointer to 2d array [duplicate]存储指向二维数组的指针[重复]
【发布时间】:2014-08-25 23:23:00
【问题描述】:

所以我在班级Map 中有一个私人成员:

char **_map;

然后我尝试将指针数组初始化为二维char 数组,如下所示:

std::vector<std::string> contents = StringUtils::split(_mapInfo.getContents(), ' ');
const int x = StringUtils::toInt(contents.at(0));
const int y = StringUtils::toInt(contents.at(1));
_map = new char[x][y];

基本上contents 向量包含两个字符串,然后我将它们转换为整数。然后我尝试初始化 map 数组,但收到此错误:

Error   1   error C2540: non-constant expression as array bound 

还有这个:

Error   2   error C2440: '=' : cannot convert from 'char (*)[1]' to 'char **'   

最后是这个:

    3   IntelliSense: expression must have a constant value 

最后一个错误引用了变量y

谁能解释发生了什么以及我该如何解决?

【问题讨论】:

标签: c++ arrays


【解决方案1】:

二维数组的初始化如下;

char **_map;

_map = new char*[rowsize];

for(int row = 0; row < rowsize; ++row)
{
    _map[row] = new char[columnsize]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 2017-06-11
    • 2012-01-26
    相关资源
    最近更新 更多