函数调用

时间:2015-06-05 11:16:59

标签: c++ arrays pointers parameters

我无法匹配我编写的函数的数据类型,

功能是:

void generate_all_paths(int size, char *maze[][size], int x, int y) {
   ...
}

这个参数大小,x和y都非常简单。我相信正是迷宫让我失望。它旨在成为一个多维大小的x大小数组,包含字母表中的字符,就像一个迷宫。

当我尝试在main中调用函数时:

int main() {
    char *exmaze[][6] = { {"#","#","#","#","#","#"},
        {"S","a","#","h","l","n"},
        {"#","b","d","p","#","#"},
        {"#","#","e","#","k","o"},
        {"#","g","f","i","j","#"},
        {"#","#","#","#","#","#"}
    };
    generate_all_paths(6, *exmaze, 1, 0);
    return 0;
}

我的IDE抱怨没有generate_all_paths函数,其参数的数据类型匹配。

我很确定我的问题主要在于我定义了exmaze但我的调整无法修复它。

有人有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分:2)

*exmaze - 为什么要去除? generate_all_paths(6, exmaze, 1, 0)将按值传递指针 - 我想在这种情况下你想要的是什么。 您还没有显示size的内容,但只是确保它的编译时间已知为常量。

此外,像这样的问题几乎总是得到建议使用像std::vector这样的标准容器,所以我不会错过它。

答案 1 :(得分:1)

在我看来,使用模板是最优雅的方式:

template<int size>
void generate_all_paths(const char *maze[][size], int x, int y) {
    ...
}

int main() {
    const char *exmaze[][6] = { {"#","#","#","#","#","#"},
        {"S","a","#","h","l","n"},
        {"#","b","d","p","#","#"},
        {"#","#","e","#","k","o"},
        {"#","g","f","i","j","#"},
        {"#","#","#","#","#","#"}
    };
    generate_all_paths(exmaze, 1, 0);
    return 0;
}

请注意 const char [] []!