Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++ 函数stoi未声明_C++_String_Pointers_Int - Fatal编程技术网

C++ 函数stoi未声明

C++ 函数stoi未声明,c++,string,pointers,int,C++,String,Pointers,Int,我试图使用stoi将字符串转换为整数,但它表示未声明。我有标准库和包含的,但它仍然说[Error]“stoi”没有在这个范围内声明 代码如下: #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string> using namespace std; int main() { string end, init; cout << "Introduc

我试图使用stoi将字符串转换为整数,但它表示未声明。我有标准库和包含的,但它仍然说[Error]“stoi”没有在这个范围内声明

代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
string end, init;
cout << "Introduction" << endl;
cout << "Start time (xx:yy)" << endl;
cin >> init;
string hours0 = init.substr(0,2);
int hours = stoi(hours0);
cout << hours << endl;
system("pause");
return 0;

}

请告诉我为什么它不起作用,或者给我第二个选项来做它。

是在C++11中引入的。确保编译器设置正确和/或编译器支持C++ 11。< /P> < P>你运行C++ 11吗?在C++ 11中添加了STOI,如果你运行在一个旧版本上,使用ATOI

STOI是C++ 11函数。如果您使用的编译器不理解C++11,那么它就无法编译

您可以使用stringstream来读取输入:

stringstream ss(hours0);
ss >> hours;

stoi从C++11开始提供。确保编译器是最新的


你可以试试atoihours0.c_str。上面的答案是正确的,但解释得不好

g++ -std=c++11 my_cpp_code.cpp
将-std=c++11添加到编译器选项中,因为您很可能使用的是旧版本的debian或ubuntu,默认情况下不使用新的c++11标准g++/gcc。我在Debian气喘上也有同样的问题


以绿色小字体在右侧显示需要c++11。

安装最新版本的TDM-GCC
这是另一个答案下评论中的链接-

,您表示在MS Windows下使用的是一个狡猾的g++版本

在这种情况下,顶部答案建议的-std=c++11仍然不能解决问题

请参阅以下讨论您的情况的帖子:

包括这个,然后你可以用

g++-Wall-std=c++11 test.cpp-o test

您还可以将cd/d%~dp0添加到与源文件位于同一目录中的.bat文件的第一行,因此您只需双击.bat文件即可进行自动编译


希望这有帮助

而不是这一行-

整小时=stoihours0

写下这个-

整小时=atoihours0.c_街

参考:

在编译代码时添加此选项:-std=c++11

g++ -std=c++11 my_cpp_code.cpp

我通过在CMakeLists.txt中添加以下行来解决此问题:


其他的人提到的,这是-STD= C++ 11问题。

< P>我在C++中的一个编程项目中遇到了这个错误,

ATOI,STOI不是G++中的旧C++库的一部分,所以编译时使用下面的选项 g++-std=c++11-o my_app_code my_app_code.cpp 在代码中包含以下文件Include
这应该考虑到错误

DevC++是IDE而不是编译器。仔细检查DVC++ C++提供的编译器版本,如果需要升级,或者添加命令行开关来启用它。因为VisualStudioC++快车只付费,这是我的最佳选择?Qt创建者对C++程序员有好处吗?@ USER 32585,Express是免费的。@克里斯我发现Express太复杂了,我不能像DeV C++那样编写代码和编译/调试。有什么建议吗?@user3258512:如果您仍然专注于使用Dev-C++,那么有一个更新的fork,名为Orwell,它似乎支持C++11:sourceforge.net/projects/orwelldevcpp/files/Setup%20Releases/他们回答如下:
g++ -std=c++11 my_cpp_code.cpp
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -Wall  -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall   -O3 -march=native")

# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
   add_definitions(-DCOMPILEDWITHC11)
   message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
   add_definitions(-DCOMPILEDWITHC0X)
   message(STATUS "Using flag -std=c++0x.")
else()
   message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()