简体   繁体   中英

error : to_string was not declared in this scope

I am compiling the code on solaris 5.11. G++ version is 4.8.2.

The same code works on Ubuntu but gives the error: 'to_string() was not declared in this scope' on solaris.

I went through many links and tried the following things:

  1. Adding 'std::' before to_string(). This gives error - 'to_string is not a member of std'
  2. Added 'std=c++11' or 'std=c++0x' while compilation.

Both the above things do not work.

Is there anything related to Solaris?

The actual code was very huge. So simulating the error in sample code below.

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

For GCC 4.8.2 the to_string functions are defined conditionally, according to the following:

#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. So the to_string definitions are absent.

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:

#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.

compile using std=c++11 as below

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

Note : your compiler must support c++11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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