无法使用 Ubuntu 解决C++构建中的声明和循环错误
Can't solve declaration and loop errors in C++ building with Ubuntu
由于这两个错误,我的项目无法构建,而且我一直无法弄清楚如何修复它们。其中一个我认为与我的GCC/G ++版本有关,但我有4.7,我很确定c ++ 11。我不确定另一个"it"声明错误。
我的代码:
#include "stdafx.h"
using namespace std;
#include <iostream>
#include <string>
void encrypt(std::string &iostr, int key) {
key %= 26;
int ch;
for (auto &it : iostr) {
ch = tolower(it);
if (!islower(ch)) {
continue;
}
ch += key;
if (ch > 'z') {
ch -= 26;
}
it = ch;
}
}
int main() {
string source;
int key = 1;
cout
<< "Paste cyphertext and press enter to shift each letter right 1";
getline(cin, source);
encrypt(source, key);
cout << source << "";
encrypt(source, key);
cout << source << endl;
cout << "Press ENTER to exit";
cin.ignore(cin.rdbuf()->in_avail() + 1);
return 0;
}
错误:
test.cpp: In function ‘void encrypt(std::string&, int)’:
test.cpp:10:13: error: ISO C++ forbids declaration of ‘it’ with no type
[-fpermissive]
test.cpp:10:18: error: range-based ‘for’ loops are not allowed in C++98 mode
这些错误是怎么回事,我该如何修复它们?提前感谢您的帮助,非常感谢。
你需要编译器选项-std=c++11
。
并评论您的第一个头文件,因为您使用的是 gcc,我认为它是特定于 Windows 的:
//#include "stdafx.h"
错误消息
error: range-based ‘for’ loops are not allowed in C++98 mode
清楚地表明您正在 C++98 模式下使用 G++。
使用命令行选项 -std=c++11
使用 C++11 模式进行编译。
相关文章:
- 在气泡排序程序中未声明错误功能
- 未在此范围内声明错误 'xy'
- 在此范围内未声明错误"Clreol"
- 为什么 C 编译器不在 for 循环体中给出重新声明错误?
- 如何修复范围内的字符串声明错误
- 如何修复C++中的"arrStud未在此范围内声明"错误
- 无法识别的模板声明错误 - 不明白我做错了什么
- C 如何从构造函数中声明(错误C2065未宣布的标识符)
- C 无法解释的类“尚未声明”错误,这是由于名称空间而导致的
- 如何解决声明错误
- 在标题文件中获取声明错误
- 异常声明错误
- "inet_ntoa"未声明错误
- #include <string> 导致很多'<blank>'尚未声明错误
- 类声明错误
- C++成员函数未声明错误时(当它看起来是错误时)
- 代码块 HelloWorld C 示例不起作用:未声明错误"ptrdiff_t"
- 头文件类成员函数声明错误:"incomplete type name in nested name specifier"
- 预期语句和预期声明错误 c++
- 针对 android 编译的问题..."未在此范围内声明"错误