C++ 在映射内部的if语句中,“在;预期的';]'&引用;出现
我目前正处于使用基于范围的for循环和map来比较数组中的项的早期阶段,但是在“if(WordSeen[Word]){”行中,会弹出以下错误: 我可能可以自己找出其余错误背后的问题,但我不明白为什么会弹出“预期”]。这是在我的函数中编写的代码:C++ 在映射内部的if语句中,“在;预期的';]'&引用;出现,c++,for-loop,mapping,C++,For Loop,Mapping,我目前正处于使用基于范围的for循环和map来比较数组中的项的早期阶段,但是在“if(WordSeen[Word]){”行中,会弹出以下错误: 我可能可以自己找出其余错误背后的问题,但我不明白为什么会弹出“预期”]。这是在我的函数中编写的代码: FString FDeclarations::CheckDuplicate() { int a = 1; TMap<FString, bool> WordSeen; //settting up the map for
FString FDeclarations::CheckDuplicate()
{
int a = 1;
TMap<FString, bool> WordSeen; //settting up the map
for (auto Word : Item) //for all words inside of the array
Word = Item [a];
if (WordSeen[Word]) {// if the word is in the array
return "something";
}
return "Nothing";
}
FString FDeclarations::CheckDuplicate()
{
INTA=1;
TMap WordSeen;//设置地图
for(auto-Word:Item)//用于数组中的所有单词
Word=项目[a];
if(WordSeen[Word]){//如果单词在数组中
返回“某物”;
}
返回“无”;
}
如果您有任何答案,请告诉我,如果您需要我发布我的代码的任何其他部分。谢谢您的帮助:)正如您问题的评论中所提到的,错误相互加剧,编译器警告您,
Word
是一个未声明的标识符
。您错过的是用于循环的。该循环由代码段中的自动格式化语法突出显示。用于深入解释
不过,我会给你一个提示,说明初学者在使用基于范围的for循环时的一个常见错误,但我强烈建议你阅读所有关于它的内容,以便更好地理解它
FString LightExample()
{
TMap<FString, bool> WordSeen = { { "One", false }, { "Two", true } };
for (auto Word // This is a copy to the 'next' element in WordSeen from begin() to end()
: WordSeen /* This is the range based container which will be iterated through */ )
{
// Word will be a pair of FString ( first part ) and bool ( second part )
if( Word.second ) // If the word was seen...
{
return Word.first; // return that word!
// NOTE: it will only ever return the first word seen
// since it is iterating in order through the map
}
}
return "Nothing";
}
FString LightExample()
{
TMap WordSeen={{“一”,false},{“二”,true};
for(auto-Word//这是WordSeen中的“next”元素从begin()到end()的副本
:WordSeen/*这是一个基于范围的容器,将在其中进行迭代*)
{
//Word将是一对FString(第一部分)和bool(第二部分)
if(Word.second)//如果看到这个词。。。
{
return Word.first;//返回该单词!
//注意:它只会返回看到的第一个单词
//因为它是按顺序在地图上迭代的
}
}
返回“无”;
}
创建一个修复错误的单词未声明的标识符"
,然后看看会发生什么。Word
不在的范围内声明,如果该代码没有多大意义。您声明一个空映射,然后迭代某个名为Item的未知容器,并将相同的潜在超出范围的值分配给一个可能的非引用变量,不做任何操作,然后希望查看空映射映射以查看其中是否有值。您可以删除所有内容,但返回“Nothing”
并获得相同的结果,另外,没有编译错误。