运行 to_string() 无法在 Linux 上运行
Can't run to_string() function on Linux
我有一些代码正在 运行ning Mac OS X 上无法在虚拟 Machine 运行宁 Linux 造币厂。这是一个简单的例子。当我 运行 它在 Mac 时,一切都很好,但是当我 运行 在 Linux 上使用相同的代码时我遇到了问题,所以我假设库我'm including 不存在,但是我应该得到一个 include 错误吗?
这是 运行 在 Mac 上的示例代码。
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
for (int i = 0; i < 10; i++){
string test = to_string(i);
cout << test << endl;
}
cout << "done" << endl;
return 0;
}
我在这里没有遇到任何问题,但是 运行 在 Linux Mint 上,我在尝试编译时得到了这个:
for.cpp: In function 'int main()':
for.cpp:7:28 error: 'to_string' was not declared in this scope
string test = to_string(i);
^
make: *** [for] Error 1
我错过了什么吗?任何帮助将非常感激!
编辑
我意识到我忘记在此处包含 <string>
并且我修复了它,但是我更改的内容(包含 <string>
)仍然无法在 Linux 上编译。我以前用过 to_string
。我对 C++ 了解很多。我也尝试添加 <cstdlib>
。再一次,这会在 Mac 上编译并且不会在 Linux 上编译。
这是我的 OSX 输出:
0
1
2
3
4
5
6
7
8
9
done
这是我在 Linux Mint 上的输出(再一次,Virtual Box,g++ make):
test.cpp: In function ‘int main()’:
test.cpp:9:28: error: ‘to_string’ was not declared in this scope
string test = to_string(i);
^
make: *** [test] Error 1
如果你不相信我,你可以自己重现这个问题。都是一样的代码,大家自己看吧。
解决方案:
我找到了更好的解决方案。出于某种原因,我读过 stdlib.h
无法在某些 linux 系统上运行。我使用不同的函数将 int
转换为 string
。
在 linux 上:
#include <stdio.h>
然后
for (int i = 0; i < 10; i++){
char buffer[10];
sprintf(buffer,"%d",i);
string stringInt = buffer;
cout << stringInt << endl;
// do whatever you want with the string
}
编辑
对于对我的解决方案投反对票的人,这是六年前的 post 基本上说了同样的话。
像这样编译你的 for.cpp
文件:
g++ -std=c++11 for.cpp
和运行它与:
./a.out
<string>
header中的to_string
函数的支持是在C++11版本的语言中添加的,所以需要告诉GCC使用那个版本.您也可以使用 c++0x
标志,例如:
g++ -std=c++0x for.cpp
而且您不必担心 <cstdlib>
,这与它无关...
如果您使用 C++11 进行编译,则 to_string()
在 <string>
中定义(但如果您使用较早版本的 C++ 进行编译,则未定义或不可靠地定义为扩展功能)。
参考:http://en.cppreference.com/w/cpp/string/basic_string/to_string
我有一些代码正在 运行ning Mac OS X 上无法在虚拟 Machine 运行宁 Linux 造币厂。这是一个简单的例子。当我 运行 它在 Mac 时,一切都很好,但是当我 运行 在 Linux 上使用相同的代码时我遇到了问题,所以我假设库我'm including 不存在,但是我应该得到一个 include 错误吗?
这是 运行 在 Mac 上的示例代码。
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
for (int i = 0; i < 10; i++){
string test = to_string(i);
cout << test << endl;
}
cout << "done" << endl;
return 0;
}
我在这里没有遇到任何问题,但是 运行 在 Linux Mint 上,我在尝试编译时得到了这个:
for.cpp: In function 'int main()':
for.cpp:7:28 error: 'to_string' was not declared in this scope
string test = to_string(i);
^
make: *** [for] Error 1
我错过了什么吗?任何帮助将非常感激!
编辑
我意识到我忘记在此处包含 <string>
并且我修复了它,但是我更改的内容(包含 <string>
)仍然无法在 Linux 上编译。我以前用过 to_string
。我对 C++ 了解很多。我也尝试添加 <cstdlib>
。再一次,这会在 Mac 上编译并且不会在 Linux 上编译。
这是我的 OSX 输出:
0
1
2
3
4
5
6
7
8
9
done
这是我在 Linux Mint 上的输出(再一次,Virtual Box,g++ make):
test.cpp: In function ‘int main()’:
test.cpp:9:28: error: ‘to_string’ was not declared in this scope
string test = to_string(i);
^
make: *** [test] Error 1
如果你不相信我,你可以自己重现这个问题。都是一样的代码,大家自己看吧。
解决方案:
我找到了更好的解决方案。出于某种原因,我读过 stdlib.h
无法在某些 linux 系统上运行。我使用不同的函数将 int
转换为 string
。
在 linux 上:
#include <stdio.h>
然后
for (int i = 0; i < 10; i++){
char buffer[10];
sprintf(buffer,"%d",i);
string stringInt = buffer;
cout << stringInt << endl;
// do whatever you want with the string
}
编辑
对于对我的解决方案投反对票的人,这是六年前的 post 基本上说了同样的话。
像这样编译你的 for.cpp
文件:
g++ -std=c++11 for.cpp
和运行它与:
./a.out
<string>
header中的to_string
函数的支持是在C++11版本的语言中添加的,所以需要告诉GCC使用那个版本.您也可以使用 c++0x
标志,例如:
g++ -std=c++0x for.cpp
而且您不必担心 <cstdlib>
,这与它无关...
如果您使用 C++11 进行编译,则 to_string()
在 <string>
中定义(但如果您使用较早版本的 C++ 进行编译,则未定义或不可靠地定义为扩展功能)。
参考:http://en.cppreference.com/w/cpp/string/basic_string/to_string