max_element() 给出非常奇怪的错误消息
max_element() is giving very weird error messages
在学习 c++ 时,我正在尝试制作一个石头剪刀布机器人(以一种非常愚蠢的方式(可能会发现对方玩家的任何模式。
#include <iostream>
#include <array>
using namespace std;
int check_winner(int a, int b) {
if (a == b) {
return 2;
}
else if (b - a == 1) {
return 1;
}
else if (b - a == -2) {
return 1;
}
else {
return 0;
}
}
int main() {
int bot_choice = rand() % 3 + 1;
int user_choice = 0;
bool run = true;
array<int, 3> rocks = {0,0,0};
array<int, 3> papers = {0,0,0};
array<int, 3> scissors = {0,0,0};
int last_choice = 0;
while (run == true) {
if (last_choice == 0) {
bot_choice = std::distance(rocks.begin(), std::max_element(rocks,rocks.end()));
}
cout << "Enter your choice from 1 to 3, or enter 0 for exiting ";
cin >> user_choice;
system("read");
if (user_choice == 0) {
return 0;
}
else {
if (last_choice == 0) {
rocks[user_choice] += 1;
}
else if (last_choice == 1) {
papers[user_choice] += 1;
}
else {
scissors[user_choice] += 1;
}
last_choice = user_choice;
cout << "Bot chose " << bot_choice << "n";
if (check_winner(user_choice,bot_choice) == 0) {
cout << "User won!n";
}
else if (check_winner(user_choice,bot_choice) == 1) {
cout << "Bot won!n";
}
else {
cout << "Draw!n";
}
}
}
}
每当我尝试运行代码时,都会出现以下错误消息:
learning_rps.cpp:30:55: error: no matching function for call to 'max_element'
bot_choice = std::distance(rocks.begin(), std::max_element(rocks,rocks.end()));
^~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2584:1: note: candidate template ignored: deduced conflicting types for parameter '_ForwardIterator'
('std::__1::array<int, 3>' vs. 'int *')
max_element(_ForwardIterator __first, _ForwardIterator __last)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2565:1: note: candidate function template not viable: requires 3 arguments, but 2 were provided
max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
^
1 error generated.
但是,如果我在用于测试的max_element
参数中添加一个零,则会出现另一个错误,在我看来,这是完全不合逻辑的:
learning_rps.cpp:30:55: error: no matching function for call to 'max_element'
bot_choice = std::distance(rocks.begin(), std::max_element(rocks,rocks.end(),0));
^~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2565:1: note: candidate template ignored: deduced conflicting types for parameter '_ForwardIterator'
('std::__1::array<int, 3>' vs. 'int *')
max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2584:1: note: candidate function template not viable: requires 2 arguments, but 3 were provided
max_element(_ForwardIterator __first, _ForwardIterator __last)
所以基本上如果我提供 2 个参数,它说我需要 3,但如果我提供 3,它说我需要 2。我对 c++ 非常陌生,所以欢迎任何关于为什么这是错误的解释以及任何其他优化。提前感谢!
你需要做:
bot_choice = std::distance(rocks.begin(), std::max_element(rocks.begin(),rocks.end()));
请参阅以下示例部分: https://en.cppreference.com/w/cpp/algorithm/max_element
相关文章:
- C++错误消息*成员参考.**初学者*
- 如何通过参数抛出错误消息
- glad 导致 glfwSwapBuffers 返回错误消息
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 重新定义预定义的 errno 错误消息 (E2BIG)
- SDL 映像:无法打开映像,仅显示错误消息
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- GCC 警告和 gcc 错误消息之间的区别
- 为 c++ cin 上的输入数无效生成错误消息
- 来自带有mbedtls TLS的Mongoose Web服务器的错误消息
- 如何使用realsense摄像头调试ROS错误消息
- C++ 概念 - 需要括号中的概念会导致 2 条冲突的错误消息
- 编译器收到错误消息(textc.exe已停止)
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 使用 c++/WinRT Direct3D UWP Game DR 模板 Direct X11 的错误消息
- max_element() 给出非常奇怪的错误消息
- 不断收到错误消息,并非所有控制路径都返回值