Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';t在Linux上运行to_string()函数_C++_Linux_Linux Mint - Fatal编程技术网

C++ Can';t在Linux上运行to_string()函数

C++ Can';t在Linux上运行to_string()函数,c++,linux,linux-mint,C++,Linux,Linux Mint,我在MacOSX上运行的一些代码无法在运行LinuxMint的虚拟机上编译。这是一个简单的例子。当我在Mac上运行它时,一切都很好,但是当我在Linux上运行相同的代码时,我会遇到问题,所以我假设我包含的库不存在,但是我应该得到一个包含错误吗 下面是在Mac上运行的示例代码 #include <iostream> #include <stdlib.h> #include <string> #include <cstdlib> using n

我在MacOSX上运行的一些代码无法在运行LinuxMint的虚拟机上编译。这是一个简单的例子。当我在Mac上运行它时,一切都很好,但是当我在Linux上运行相同的代码时,我会遇到问题,所以我假设我包含的库不存在,但是我应该得到一个包含错误吗

下面是在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上编译。我以前用过
来编辑字符串。我在C++中知道很多。我还尝试添加
。同样,它在Mac上编译,在Linux上不编译

以下是我的OSX输出:

0
1
2
3
4
5
6
7
8
9
done
这是我在LinuxMint上的输出(同样,是Virtual Box,g++make):

如果你不相信我,你可以自己复制这个问题。这是相同的代码,如果你想看,你可以自己看

解决方案:

我找到了更好的解决办法。出于某种原因,我读到了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
}
for(int i=0;i<10;i++){
字符缓冲区[10];
sprintf(缓冲区,“%d”,i);
字符串stringInt=buffer;

不能像这样编译.cpp的
文件:

g++ -std=c++11 for.cpp
并使用以下工具运行它:

./a.out
在该语言的C++11版本中添加了对
标题中
to_string
函数的支持,因此您需要告诉GCC使用该版本。您也可以使用
C++0x
标志,例如:

g++ -std=c++0x for.cpp
您不必担心
,这与此无关……
如果使用C++11编译,则在
中定义了
to_string()
(但如果使用早期版本的C++,则未定义或不可靠地定义为扩展功能)


参考文献:

是在<代码> <代码>中。应该在C++中而不是这个<代码>包含。你应该使用:<代码>包含< <代码> > @ Galik,谢谢你的回复。我试过了(尝试使用两个在OSX上工作良好的LIBA)。以及将
替换为
。由于某些原因,它仍然无法在我的Linux虚拟机上编译。您使用的是哪个版本的gcc?(顺便说一句,清楚地区分编译和运行是很有帮助的——如果它没有在Linux上编译,就没有什么可运行的,但有几个地方你认为这是运行时错误)。gcc版本4.8.2(Ubuntu 4.8.2-19ubuntu1)也许六年前的解决方案也应该被否决?你读得完全错了。
在linux上运行得很好,它只是没有包含非标准的
itoa()
函数。@ajaysinghnegi:我的jpcre2库中也有一个。你可能会在那里有所了解。
./a.out
g++ -std=c++0x for.cpp