这可能是一个愚蠢的问题。但我注意到这一行没有在我的机器上编译,唯一的区别是安装的 g++ 版本。 我将版本更新回 11,它又开始工作了。我不认为这有什么问题。 我可以得到一些帮助吗?
char shipLetters[] = { 'A', 'B', 'S', 'C', 'D' };
这适用于 g++11,但不适用于 g++13
char seletion;
cin >> selection;
index = std::find(begin(shipLetters), end(shipLetters), selection);
添加了可运行的示例。
#include <iostream>
using namespace std;
int main() {
char shipLetters[] = { 'A', 'B', 'S', 'C', 'D' };
char selection;
cin >> selection;
char *index = std::find(begin(shipLetters), end(shipLetters), selection);
cout << index << endl;
return 0;
}
看起来我只需要明确地包含算法即可。我不确定为什么运行 g++11 时没有出错,但它现在可以工作了。
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
char shipLetters[] = { 'A', 'B', 'S', 'C', 'D' };
char selection;
cin >> selection;
char *index = std::find(begin(shipLetters), end(shipLetters), selection);
cout << index << endl;
return 0;
}