我正在Solaris 5.11上编译代码。 G++版本是4.8.2。
相同的代码可以在 Ubuntu 上运行,但会出现错误:在 Solaris 上,“to_string() 未在此范围内声明”。
我浏览了很多链接并尝试了以下操作:
- 在 to_string() 之前添加“std::”。这会产生错误 - 'to_string 不是 std 的成员'
- 编译时添加了“std=c++11”或“std=c++0x”。
以上两件事都不起作用。
有什么与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
最佳答案
对于 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。
关于c++ - 错误: to_string was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053711/