我在int main中创建了一个传递给GenerateMap的数组,然后GenerateMap函数遍历数组并分配每个项目。在分配了每个项目之后,我希望它返回到int main,以便我可以在以后使用它,但我一直在收到错误。经过几次尝试后,我通过参考传递阵列来跟随Thenewboston的指示。但我仍然在代码中遇到错误。有人可以向我解释我应该做什么在另一个函数中创建地图吗?
错误
prog.cpp: In function 'int main()': prog.cpp:22:17: error: cannot convert 'std::string (*)[MapDimension][MapDimension] {aka std::basic_string<char> (*)[MapDimension][MapDimension]}' to 'std::string** {aka std::basic_string<char>**}' for argument '1' to 'void GenerateMap(std::string**)' GenerateMap(&Map); ^ prog.cpp: In function 'void GenerateMap(std::string**)': [NEW ERROR] prog.cpp:70:31: error: no match for 'operator*' (operand type is 'std::string {aka std::basic_string<char>}') cout <<"[" << *Map[i][j] << "]" << " ";
CODE
int main() {
string Map[MapDimension][MapDimension];
GenerateMap(&Map);
}
void GenerateMap(string *Map[]) {
srand(time(NULL));
string x;
for (int i = 0; i <= MapDimension-1; ++i) {
for (int j = 0; j <= MapDimension-1; ++j)
{
int random = rand() % 6;
switch (random)
{
case 0:
x = " D ";
break;
case 1:
x = " F ";
break;
case 2:
x = " P ";
break;
case 3:
x = " M ";
break;
case 4:
x = " L ";
break;
case 5:
x = " S ";
break;
}
Map[i][j] = x;
cout <<"[" << *Map[i][j] << "]" << " ";
}
cout << endl;
}
}
答案 0 :(得分:1)
你需要一个分号:
string Map[MapDimension][MapDimension]
应该是
string Map[MapDimension][MapDimension];