范围For循环帮助:在“:”之前的预期初始化程序令牌 [英] Range For loop help: Expected initializer before ":" token

查看:495
本文介绍了范围For循环帮助:在“:”之前的预期初始化程序令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

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

使用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 :在函数'int main()':
test_strings.c:14:错误:预期的初始化前':'token
test_strings.c:19:错误: b $ b test_strings.c:19:error:expected')'before'return'

程序从教科书,所以我虽然它是一个拼写错误,但在检查和尝试在网上搜索和更新我的gcc错误提醒。

解决方案

根据 GCC页面中的C ++ 0x / C ++ 11支持页面,您需要运行gcc 4.6来获取范围 - 功能。



4.6更改页面包含:


改进了对即将到来的C ++ 0x ISO C ++标准的实验支持,包括对constexpr的支持(感谢Gabriel Dos Reis和Jason Merrill),nullptr(感谢Magnus Fromreide) ,noexcept,无限制联合,基于范围的循环(感谢Rodrigo Rivas Costa),不透明枚举声明(感谢Rodrigo),隐式删除函数和隐式移动构造函数。


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


I am completely new to C++ and I am now following the C++ Primer book.

I wrote a small example about strings, here is the code:

#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;
}

I am on Linux with GCC version 4.4.6 and I tried to compile this code with:

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

but got the following errors:

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'

I copied the program from the textbook, so I though it was a misspelling but after a check and trying searching on the web and updating my gcc the error reminds. Help will be greatly appreciated, thanks in advance.

解决方案

As per the C++0x/C++11 Support in GCC page, you need to be running gcc 4.6 to get the range-for feature.

The 4.6 changes page contains:

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.

Since you're running gcc 4.4.6, it's not available to you.

这篇关于范围For循环帮助:在“:”之前的预期初始化程序令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆