4

出于某种原因或其他原因,当尝试在 mingw 上的 G++ 中编译以下代码时

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

int main( int argc, char **argv )
{
    std::string s( "Hello, World!" );

    decltype( s.size(  ) ) punct_cnt = 0;

    for ( auto c : s )
    {
        if ( ispunct( c ) )
            ++punct_cnt;
    }

    std::cout << punct_cnt << " punctuation characters in " << s << std::endl;

    return 0;
}

我收到以下错误

test.cpp: In function 'int main(int, char**)':
test.cpp:9:23: error: 'decltype' was not declared in this scope
test.cpp:9:25: error: expected ';' before 'punct_cnt'
test.cpp:11:13: error: 'c' does not name a type
test.cpp:17:2: error: expected ';' before 'std'
test.cpp:17:15: error: 'punct_cnt' was not declared in this scope
test.cpp:19:2: error: expected primary-expression before 'return'
test.cpp:19:2: error: expected ')' before 'return'

decltype我已经检查过,g++ 编译器的版本是 4.7.2,除了更改为之外,任何人都知道如何解决std::string::size_type

4

1 回答 1

7

decltype是一个 c++11 功能。你需要这样调用 gcc

g++ -std=c++11
于 2012-12-19T17:57:55.893 回答