C++ C++;邪恶刽子手的执行问题
我正在尝试实现一个版本的邪恶刽子手,游戏会根据最难猜的单词来改变单词。然而,我得到了一个错误,我只是无法发现它。这可能是件愚蠢的事情,但我感觉我已经盯着它看了好几个小时了。在崩溃之前,我通过打印语句将其缩小为:C++ C++;邪恶刽子手的执行问题,c++,if-statement,vector,crash,C++,If Statement,Vector,Crash,我正在尝试实现一个版本的邪恶刽子手,游戏会根据最难猜的单词来改变单词。然而,我得到了一个错误,我只是无法发现它。这可能是件愚蠢的事情,但我感觉我已经盯着它看了好几个小时了。在崩溃之前,我通过打印语句将其缩小为: vector<string> newWords; bool inword=false; int place=0; cout<<"here 1"; if(!remove){//remove true so only keep word
vector<string> newWords;
bool inword=false;
int place=0;
cout<<"here 1";
if(!remove){//remove true so only keep words that dont have letter in them.
cout<<" in if(remove) \n";
for(int i=0;i<wordsLeft.size();i++){
cout<<" in first loop \n";
for(int n=0;n<(wordsLeft[i].length())&&!inword;n++){
cout<<"in second loop n: "<<n<<" i: "<<i<<" inword: "<<inword<<" length: "<<wordsLeft[i].length()<<endl;
if(wordsLeft[i].at(n)==letter){
inword=true;
}
}
if(!inword){//if false then add word meaning words not containing letter array
newWords[place]=wordsLeft[i];
place+=1;
}
else{
inword=false;
}
}
cout<<"here 2";
}
else{//remove false so only keep words that have letter in them.
cout<<"here 3";
for(int i=0;i<wordsLeft.size();i++){
for(int n=0;n<(wordsLeft[i].length())&&!inword;n++){
if(wordsLeft[i].at(n)==letter){
inword=true;
}
}
if(inword){//if true then add word meaning words containing letter array
newWords[place]=wordsLeft[i];
place+=1;
inword=false;
}
}
cout<<"here 3.5";
}
cout<<"here 4";
index=place;
wordsLeft=newWords;
}
向量新词;
bool-inword=false;
int place=0;
cout我还不能添加注释,但根据您的注释它表示wordsLeft未在作用域中声明,这意味着第一个for循环中的wordsLeft
无法访问。您可能已经在另一个函数的其他地方定义了该变量,或者它可能是私有的
在定义wordsLeft的地方发布,下一次:发布错误消息/代码“我遇到了一个错误”。。。那到底是什么?@Cyber它是说wordsLeft没有在范围内声明,你在这段代码之前是否声明了wordLeft
?因为它没有申报。另外,如果wordsLeft
和newWords
是std::vectors
您应该使用std::vector.iterator()
而不是整数。您将“wordsLeft”的声明放在哪里了?