error: name lookup of 'first' changed for ISO 'for' scoping [-fpermissive]
来源:互联网 发布:韩侂胄 知乎 编辑:程序博客网 时间:2024/05/17 01:59
//读一组整数到vector对象,计算首尾配对元素的和并输出
//使用迭代器访问vector中的元素
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> ivec;
int ival;
//读入数据到vector对象
cout<<"Enter numbers(Ctrl+Z to end):"<<endl;
while(cin>>ival)
ivec.push_back(ival);
//计算首尾配对元素的和并输出
if(ivec.size()==0){
cout<<"No element?!"<<endl;
return -1;
}
cout<<"Sum of each pair of counterpart element in the vector:"
<<endl;
vector<int>::size_type cnt=0;
for(vector<int>::iterator first=ivec.begin(),last = ivec.end()-1;first<last;++first,--last){
cout<<*first+*last<<"/t";
++cnt;
if(cnt %6 ==0) //每行输出六个和
cout<<endl;
}
if(first==last) //提示居中元素没有求和
cout<<endl
<<"The center element is not been summed"
<<"and its value is"
<<*first<<endl;
return 0;
}
在ubuntu下用g++编译会出现如下错误:
/mywork/mytest/iterator/main.cpp:88: error: name lookup of 'first' changed for ISO 'for' scoping [-fpermissive]
/mywork/mytest/iterator/main.cpp:88: (if you use '-fpermissive' G++ will accept your code)
错误的原因: for循环中在初始化时同时定义的变量的作用域范围的一个问题。ISO/ANSI C++ 把在此定义的变量的作用域范围限定在 for 循环体内,也就是说,在循环体之外这个变量是无效的。但是编译的时候可以加参数-fpermissive,就没有问题了。如果不加这个参数,直接把for中定义并初始化的变量,定义与初始化分离:
vector<int>::iterator first;
vector<int>::iterator last;
for( first=ivec.begin(),last = ivec.end()-1;first<last;++first,--last){
cout<<*first+*last<<"/t";
++cnt;
if(cnt %6 ==0) //每行输出六个和
cout<<endl;
}
- error: name lookup of 'first' changed for ISO 'for' scoping [-fpermissive]
- error:name lookup of "c1 "changed for ISO 'for' scoping
- name lookup of `i' changed for new ISO `for' scoping
- name lookup of 'res' changed for new ISO 'res' scoping
- c++编译错误提示[Error] name lookup of 'i' changed for ISO 'for' scoping
- 自学QT之报错: name lookup of 'i' changed for ISO 'for' scoping
- Qt中"look up 'i' Changed for ISO 'for' scoping" 错误及解决
- 编译错误:error: default argument given for parameter 1 of ‘’ [-fpermissive]
- Name lookup for Class Member Declarations
- C++编译错误:error: default argument given for parameter... [-fpermissive]
- 【GCC调试程序C语言问题】error: initializer-string for array of chars is too long [-fpermissive]
- error: ISO C++ forbids declaration of ‘xxx’ with no type [-fpermissive]
- error: ISO C++ forbids declaration of ‘xxx’ with no type [-fpermissive]
- 一般c++编译出现"name lookup of '变量名' changed [enabled by default]"的警告
- error C4996: 'getpid': The POSIX name for this item is deprecated. Instead, use the ISO C++ conforma
- PostgreSQL ERROR: cache lookup failed for function 98810
- SSM+Mevan报JNDI lookup for name [spring.profiles.active] threw NamingException with message: Name [sp
- ISO C++ forbids declaration of ‘Contack’ with no type [-fpermissive]
- Hashtable、synchronizedMap、ConcurrentHashMap 比较
- 网络字节转换函数 htons(), htonl(), ntohs(), ntohl()
- java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@41a7f048
- 保持坦然平和的心态!
- 动网7.0sp2 cookie欺骗原理
- error: name lookup of 'first' changed for ISO 'for' scoping [-fpermissive]
- 在Hibernate 4.3中如果使用了@Table注解,在获取session的时候会抛出如下异常解决办法
- HDU 2047 阿牛的EOF牛肉串
- 自动控制专业为什么要学习计算机语言?
- [c/c++ ]字节序与大小端转换
- The connection to adb is down, and a severe error has occured.
- Bash脚本之for命令
- socket网络编程之select模型
- 汇编jmp指令用法