首页 > 解决方案 > 调用 (list 和 push_front) 没有匹配的函数

问题描述

对于游戏策划者,我需要生成所有可能的猜测。我想把它们放在list<char[]>. 我仍然收到这个错误push_front

有人可以帮助我吗?

void Mastermind::genereercodes (int getal, list<char[]> mogelijkeGok)
{
char gok[aantGaatjes+1]; //aantGaatjes is an integer
for (int i = 0; i < aantGaatjes; i++){
    gok[i] = (getal % aantKleuren) + '0'; //aantKleuren is an integer
    getal = getal / aantKleuren;
} // for
mogelijkeGok.push_front (&gok);

}   // genereercodes

这是我得到的错误:

mastermind.cc: In member function ‘void Mastermind::genereercodes(int,std::__cxx11::list<char []>)’: 
mastermind.cc:200:32: error: no matching  unction for call to ‘std::__cxx11::list<char []>::push_front(char *)[(((Mastermind*)this)->Mastermind::aantGaatjes + 1)])’
mogelijkeGok.push_front (&gok);

标签: c++

解决方案


您无缘无故地使用 C 风格的数组。有两种可能的替代品:std::arraystd::vector。既然我们知道:

用户需要放弃aantGaatjes的值,所以在程序运行之前你是不知道值的。

这只留std::vector在桌子上。需要修复的另一件事是使用 out-parameters,除了在代码中错误地执行之外,无论如何都是不好的做法。然后代码变为:

std::vector<char> Mastermind::genereercodes (int getal)
{
    std::vector<char> gok(aantGaatjes+1);

    for (char& e : gok) {
        e = (getal % aantKleuren) + '0';
        getal = getal / aantKleuren;
    }

    return gok;
}

这不处理追加到列表,也不应该需要。拥有该列表的调用者可以像这样简单地调用它:

mogelijkeGok.push_front(genereercodes(getal));

推荐阅读