Mingw g++ 4.7.2中的未声明类型。

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'

我已经检查过,g++编译器的版本是4.7.2,除了将decltype更改为std::string::size_type,还有其他解决方法吗?请注意不要删除HTML标签。

1个回答

7

decltype 是 C++11 的一个特性。您需要这样调用 GCC:

g++ -std=c++11

@ctor 尽量避免使用 decltype,它并不像看起来那么好,基本上标准在使用 decltype 时不能确保任何事情。 - user1824407

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接