C++ 错误:“i”的名称查找更改为 ISO“for”范围[-fpermissive]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24117264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
提问by user3704747
Q. Write a program that 'bleeps' out words that you don't like; that is, you read in words using cin and print them again on cout. If a word is among a few you have defined, you write out BLEEP instead of that word. (stroustrup's c++ book)
问:编写一个程序,将您不喜欢的单词“滴”出;也就是说,您使用 cin 读入单词并在 cout 上再次打印它们。如果一个词在你定义的几个词中,你写出 BLEEP 而不是那个词。(stroustrup 的 C++ 书)
Here's the code I wrote:
这是我写的代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }
int main()
{
vector<string> disliked;
disliked.push_back("Broccoli");
disliked.push_back("Carrots");
disliked.push_back("Tomatoes");
disliked.push_back("Asparagus");
vector<string> words;
string word;
while (cin >> word) {
words.push_back(word);
}
for (int i = 0; i < words.size(); ++i) {
cout << words[i] << "\t"; //i used it to see if the program was working
}
for (int j = 0; j < disliked.size(); ++j) {
cout << disliked[j] << "\t";
}
for (i = 0; i < j; ++i) {
if (words[i] == disliked[j]) {
cout << "BLEEP";
}
else {
}
}
}
I think the problem arises due to my final for loop, but I don't understand what is to be done.
我认为问题是由于我的最终 for 循环而出现的,但我不明白要做什么。
Here is the error I got:
这是我得到的错误:
bleep.cpp: In function ‘int main()':
bleep.cpp:27:8: error: name lookup of ‘i' changed for ISO ‘for' scoping [-fpermissive]
bleep.cpp:27:8: note: (if you use ‘-fpermissive' G++ will accept your code)
bleep.cpp:27:19: error: name lookup of ‘j' changed for ISO ‘for' scoping [-fpermissive]
回答by Rakib
The problem is:
问题是:
for (i = 0; i < j; ++i) {
if (words[i] == disliked[j]) {
cout << "BLEEP";
}
else {
}
}
Here you are using i
and j
without declaring them. The previous declarations have only the block scope in which you are declaring. Either you have to redeclare them or if u want to use previous values, declare them above the first for
loop.
在这里您正在使用i
并且j
没有声明它们。前面的声明只有您声明的块作用域。您必须重新声明它们,或者如果您想使用以前的值,请在第一个for
循环上方声明它们。
回答by menirus95
You have declared the variable iand jin the for loop, hence because of the scope issue, you cannot access the variables in another for loop.
您已经在 for 循环中声明了变量i和j,因此由于范围问题,您无法访问另一个 for 循环中的变量。
From what I have understood, in the final for loop, you are checking whether each word in the wordsarray is equal to one of the words in dislikedarray. To do this, you need to use two for loops as follows:
据我所知,在最后的 for 循环中,您正在检查words数组中的每个单词是否等于disliked数组中的单词之一。为此,您需要使用两个 for 循环,如下所示:
for(int i=0; i<words.size(); i++){
for(int j=0; j<disliked.size(); j++){
if(words[i] == disliked[j]){
words[i] = "BLEEP"; //This step replaces the disliked word with "BLEEP"
}
}
}
Note : If you declare int i,j;
before using them in the loops, then you need not declare them again inside any of the for loops.
注意:如果您int i,j;
在循环中使用它们之前声明,那么您不需要在任何 for 循环中再次声明它们。