I'm trying to use stoi
to convert a string to an integer, however it says it's not declared. I have the standard library and the <string>
included, but it still says [Error] 'stoi' was not declared in this scope
The code is the following:
#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;
}
Either tell me why it isn't working, or give me a second option to do it, please.
I came across this error while working on a programming project in c++,
This should take care of the errors
Install the latest version of TDM-GCC here is the link-http://wiki.codeblocks.org/index.php/MinGW_installation
stoi
is a C++11 function. If you aren't using a compiler that understands C++11, this simply won't compile.
You can use a stringstream
instead to read the input:
stringstream ss(hours0);
ss >> hours;
The answers above are correct, but not well explained.
g++ -std=c++11 my_cpp_code.cpp
Add -std=c++11 to your compiler options since you are most likely using an older version of debian or ubuntu which is not using by default the new c++11 standard of g++/gcc. I had the same problem on Debian Wheezy.
http://en.cppreference.com/w/cpp/string/basic_string/stol
shows in really small writing to the right in green that c++11 is required.
Are you running C++ 11? stoi was added in C++ 11, if you're running on an older version use atoi()
I managed to fix this problem by adding the following lines to my CMakeLists.txt
:
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()
As mentioned by the other fellows, that is the -std=c++11
issue.
Add this option: -std=c++11
while compiling your code
g++ -std=c++11 my_cpp_code.cpp
#include <algorithm>
Include this and then you can compile it using...
g++ -Wall -std=c++11 test.cpp -o test
You can also add "cd /d %~dp0" as the first line of a .bat file in the same directory as your source file so all you have to do is double click on the .bat file for an "automated" compilation.
Hope this helps!
In comments under another answer, you indicated you are using a dodgy version of g++
under MS Windows.
In this case, -std=c++11
as suggested by the top answer would still not fix the problem.
Please see the following thread which does discuss your situation: std::stoi doesn't exist in g++ 4.6.1 on MinGW
stoi is available "since C++11". Make sure your compiler is up to date.
You can try atoi(hours0.c_str()) instead.
instead of this line -
int hours = stoi(hours0);
write this -
int hours = atoi(hours0.c_str());
Reference : int atoi(const char *str)
Source: Stackoverflow.com