我已经为年轻观众创建了一个游戏,并试图过滤掉亵渎和冒犯性的名字
#include <iostream>
#include <vector>
bool isBanned( std::string text ) {
std::vector bannedSent = {
"Profanity1",
"Profanity2",
"Profanity3",
"Profanity4"
};
for(auto &i : bannedSent) {
if(text == i) { return true; }
}
return false;
}在std::vector行上,我得到了一个关于“模板参数”的编译器错误,这是什么意思?
发布于 2016-03-08 08:20:47
你需要为你的向量提供模板参数。由于您持有字符串,因此需要像这样声明它:
std::vector< std::string > bannedSent = {
"Gosh",
"Golly",
"Jeepers",
"Troll"
};发布于 2016-03-08 21:37:11
实际上,最简单的解决方案是不指定类型。编译器已经有了一个不错的想法,并且您已经知道了关键字:
auto bannedSent = {
"Profanity1",
"Profanity2",
"Profanity3",
"Profanity4"
};
for(auto i : bannedSent) { ...副作用:这避免了在每次调用中构造4个std::string对象。
请注意,您在前面使用了auto& i。这是一个错误,您并不打算更改bannedSent。
发布于 2016-03-08 08:19:19
If应为std::vector<std::string>
bool isBanned( std::string text ) {
std::vector<std::string> bannedSent = {
...
}
}https://stackoverflow.com/questions/35856659
复制相似问题