gpt4 book ai didi

c++ - 范围 For 循环帮助 : Expected initializer before ":" token

转载 作者:搜寻专家 更新时间:2023-10-31 00:40:47 26 4
gpt4 key购买 nike

我是 C++ 的新手,现在正在阅读 C++ 入门书。

我写了一个关于字符串的小例子,代码如下:

#include <iostream>
#include <string>
#include <cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
string s("Hello World");

for (auto &c : s)
c = toupper(c);

cout << s << endl;

return 0;
}

我在 Linux 上使用 GCC 4.4.6 版,我尝试用以下代码编译这段代码:

g++ test_strings.c -std=c++0x

但出现以下错误:

test_strings.c: In function 'int main()':
test_strings.c:14: error: expected initializer before ':' token
test_strings.c:19: error: expected primary-expression before 'return'
test_strings.c:19: error: expected ')' before 'return'

我是从教科书上复制程序的,所以我虽然是拼写错误,但在检查并尝试在网络上搜索并更新我的 gcc 后,错误提醒了。非常感谢您的帮助,在此先致谢。

最佳答案

根据 the C++0x/C++11 Support in GCC page ,您需要运行 gcc 4.6 才能获得范围功能。

4.6 changes page包含:

Improved experimental support for the upcoming C++0x ISO C++ standard, including support for constexpr (thanks to Gabriel Dos Reis and Jason Merrill), nullptr (thanks to Magnus Fromreide), noexcept, unrestricted unions, range-based for loops (thanks to Rodrigo Rivas Costa), opaque enum declarations (thanks also to Rodrigo), implicitly deleted functions and implicit move constructors.

由于您运行的是 gcc 4.4.6,因此您无法使用它。

关于c++ - 范围 For 循环帮助 : Expected initializer before ":" token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14117918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com