2011-08-15 13 views
6

G ++の新しいC++ 0x機能のいくつかを試していました。 Lambdas、autoなどの新しい機能は魅力的に機能しましたが、範囲ベースのforループはコンパイルできませんでした。私はまた、gnu++0xを試してみましたが、出力は同じであったG ++はC++ 0xの範囲ベースのループをコンパイルしません

g++ test.cpp -std=c++0x 

:私はそれを

#include <iostream> 
#include <vector> 

int main() 
{ 
    std::vector<int> data = { 1, 2, 3, 4 }; 

    for (int datum : data) 
    { 
     std::cout << datum << std::endl; 
    } 
} 

コンパイル:これは私がテストされたプログラムです。

これは出力した:あなたの助けを事前に

test.cpp: In function ‘int main()’: 
test.cpp:8:21: error: expected initializer before ‘:’ token 
test.cpp:12:1: error: expected primary-expression before ‘}’ token 
test.cpp:12:1: error: expected ‘;’ before ‘}’ token 
test.cpp:12:1: error: expected primary-expression before ‘}’ token 
test.cpp:12:1: error: expected ‘)’ before ‘}’ token 
test.cpp:12:1: error: expected primary-expression before ‘}’ token 
test.cpp:12:1: error: expected ‘;’ before ‘}’ token 

感謝。

EDIT:GCCバージョン4.5.2を使用していますが、これは今では古すぎます。

+2

? – pmr

答えて

14

ループの範囲ベースを取得するには、GCC 4.6以上が必要です。使用しているgccのバージョン

GCC's C++0x status

$ cat for.cpp 
#include <iostream> 
int main() 
{ 
    for (char c: "Hello, world!") 
    std::cout << c; 
    std::cout << std::endl; 
    return 0; 
} 
$ g++ -std=c++0x -o for for.cpp 
$ ./for 
Hello, world! 
$ g++ --version 
g++ (GCC) 4.6.1 20110325 (prerelease) 
+0

ありがとう!私はこのバージョンが私のUbuntuリポジトリにないことを知っているので、手動でインストールする必要があるかもしれないと思います。 – rovaughn

関連する問題