错误:未在此范围内声明to_string

时间:2017-11-01 11:23:13

标签: c++ c++11 g++ solaris

我正在编译solaris 5.11上的代码。 G ++版本是4.8.2。

相同的代码适用于Ubuntu,但会出现错误:' to_string()未在此范围内声明'在solaris上。

我浏览了很多链接并尝试了以下方法:

  1. 添加' std ::'在to_string()之前。这会产生错误 - ' to_string不是std'
  2. 的成员
  3. 添加了' std = c ++ 11'或者' std = c ++ 0x'编译时。
  4. 以上两点都不起作用。

    是否有与Solaris相关的内容?

    实际代码非常庞大。所以模拟下面的示例代码中的错误。

    temp.cpp

    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        string str;
        int i = 10;
        str = "john age is " + to_string(i);
        cout << str;
        return 0;
    }
    

    命令:g ++ temp.cpp -std = c ++ 0x -o temp

2 个答案:

答案 0 :(得分:4)

对于GCC 4.8.2,to_string函数是有条件定义的,根据以下内容:

#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
     && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))

GLIBCXX_USE_C99宏取决于操作系统支持的大量C99函数,因此在Solaris上构建GCC时可能找不到必要的C99库函数。因此,to_string定义不存在。

在当前版本的GCC中,条件更精细,并检查C99函数是否在C ++ 98模式和C ++ 11中定义,因此没有任何C99函数不会禁用一切:

#if __cplusplus >= 201103L
//...
#if _GLIBCXX_USE_C99_STDIO

无法将这些改进反向移植到GCC 4.8,因此您可能需要更新到至少GCC 6。

答案 1 :(得分:0)

使用std = c ++ 11进行编译,如下所示

g ++ -std = c ++ 11 filename.cc

注意:您的编译器必须支持c ++ 11