c++ - 调用 (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 风格的数组。有两种可能的替代品:std::array
和std::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));
推荐阅读
- python - 硒多进程python3
- swift - Navigationbartitle 显示一个白框
- android - Android GitLab CI/CD 配置文件问题
- postgresql - 是否可以在 keystoneJS 6 上使用 PostgresQL 视图
- c# - MVC CORE 5 Boolean Radio Button asp-for Binding for Model.Property 未检查 Get
- javascript - 如何在 Ember Js 中的 If 语句中调用动作
- node.js - firebase 云函数示例代码错误
- react-native - React Native Api 和 Android 登录面临问题更新 android 版本失败?
- python - Google Ads API 指标成本、每次点击费用等太大了
- javascript - 是否可以通过 JavaScript 甚至 CSS 确定形状是否在 SVG 中的特定掩码下?