gpt4 book ai didi

linux - 无法在 LINUX 的 C++ 中将 STRING 转换为 INT

转载 作者:IT王子 更新时间:2023-10-29 01:02:18 25 4
gpt4 key购买 nike

我尝试了很多方法,详见此处:http://www.cplusplus.com/forum/general/13135/

如果我在 Windows 上运行该文件,它们中的大多数都可以工作,但是当我尝试在 LINUX 上运行时,它们都不起作用。例如,我尝试这样做:

  string str = "123";
int sp;
istringstream ( str ) >> sp;

但它给了我错误:“无效使用不完整类型‘struct std::istringstream’/usr/include/c++/4.4/iosfwd:67: 错误:‘struct std::istringstream’ 的声明”

其他选项是“atoi”,但它表示“atoi 未在此范围内定义。”

知道为什么会这样吗?

最佳答案

至于 atoi() 函数,您需要包含 cstdlib ( #include <cstdlib> ) header 。

然后你可以像这样使用它:

string str= "123"; 
int sp = atoi(str.c_str());

它将告诉字符串对象充当指向 C 风格字符串的 const char*(最有可能被称为带有零终止符\0 的字符串)。

关于linux - 无法在 LINUX 的 C++ 中将 STRING 转换为 INT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13346313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com