简体   繁体   English

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

[英]error : to_string was not declared in this scope

I am compiling the code on solaris 5.11. 我正在编译solaris 5.11上的代码。 G++ version is 4.8.2. G ++版本是4.8.2。

The same code works on Ubuntu but gives the error: 'to_string() was not declared in this scope' on solaris. 相同的代码可在Ubuntu上使用,但会出现错误:在solaris上未在此范围内声明“ to_string()”。

I went through many links and tried the following things: 我浏览了许多链接并尝试了以下操作:

  1. Adding 'std::' before to_string(). 在to_string()之前添加“ std ::”。 This gives error - 'to_string is not a member of std' 这给出了错误-'to_string不是std的成员'
  2. Added 'std=c++11' or 'std=c++0x' while compilation. 编译时添加了“ std = c ++ 11”或“ std = c ++ 0x”。

Both the above things do not work. 以上两种方法均无效。

Is there anything related to Solaris? 是否与Solaris有关?

The actual code was very huge. 实际的代码非常庞大。 So simulating the error in sample code below. 因此,在下面的示例代码中模拟错误。

temp.cpp 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;
}

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

For GCC 4.8.2 the to_string functions are defined conditionally, according to the following: 对于GCC 4.8.2, to_string函数根据以下条件有条件地定义:

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

The GLIBCXX_USE_C99 macro depends on a large number of C99 functions being supported by the OS, so presumably the necessary C99 library functions were not found when building GCC on Solaris. GLIBCXX_USE_C99宏取决于操作系统支持的大量C99函数,因此,大概是在Solaris上构建GCC时未找到必需的C99库函数。 So the to_string definitions are absent. 因此,没有to_string定义。

In current versions of GCC the condition is more fine-grained, and checks whether the C99 functions are defined in C++98 mode and C++11, so that the absence of any C99 function doesn't disable everything: 在当前版本的GCC中,条件更为细化,并检查C99函数是否在C ++ 98模式和C ++ 11中定义,以便缺少任何C99函数不会禁用所有功能:

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

It's not possible to backport these improvements to GCC 4.8, so you might need to update to at least GCC 6. 无法将这些改进移植到GCC 4.8,因此您可能需要至少更新到GCC 6。

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

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

Note : your compiler must support c++11 注意:您的编译器必须支持c ++ 11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM