'stoi' was not declared in this scope after using -std=c++11 - c++

Most probably this is weird, but when I got this error that stoi wasn't declared in this scope, I smiled because I am familiar with this error and it's solution.
I checked this option have g++ follow the c++11 ISO c++ language standard [-std=c++11] in compiler settings of Code Blocks (16.01, with MinGW) and tried recompiling it, but surprisingly it didn't work and the same error persisted. I tried re-installing CodeBlocks but that didn't work.
Also, I tried with windows power shell and command prompt with g++ math_handler.cpp -std=c++11 but got the same error.
What am I doing wrong?
the code is here:
#include<string>
using namespace std;
int main()
{
string body="456";
int i=stoi(body);
}
Note:
I tried with -std=c++0x and g++ too.
the same problem with to_string() function.
gcc version 4.9.2 (tdm -1)

Okay, I found that it is a known bug in MinGW bundled with CodeBlocks. I found the solution here.
Download mingw-to-string-gcc47.zip which contains three patched
header files. (Original patches: wchar.h, stdio.h, os_defines.h)
Copy wchar.h and stdio.h from the include directory in the zip file
to the following directory (overwrite): C:\mingw\include (replace
C:\mingw\ with the appropriate directory)
Copy os_defines.h to the following directory (overwrite):
C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace
C:\mingw\ with the appropriate directory) (replace 4.7.0 with the
correct version number)

Did you include the required header file?
#include <string>
stoi is also in the std namespace so:
std::stoi()
or:
using namespace std;

Related

Xcode 11.1: iostream' file not found

I just updated my MacBook Pro to macOS Catalina 10.15, and tried to compile and run a C++ command line program, but I had a problem which didn’t exist on previous versions;
This is simply the code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}
The code compiles and outputs the expected, but still the Xcode says:
fatal error: 'iostream' file not found
I tried changing the Build Settings/C++ Standard Library to libstdc++, but a warning says:
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead
And the same iostream error still exists.
I'm compiling from the command line, and none of the answers listed here (or elsewhere) worked for me.
What does seem to work (so far) is to add the following to .profile or whatever script your terminal uses to start up: (zsh, csh, bash, etc.)
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
You will probably have to change MacOSX10.15.sdk whenever you upgrade your operating system.
C_INCLUDE_PATH and CPLUS_INCLUDE_PATH are options for the clang toolchain rather than MacOS environment, so hopefully this solution will work long-term, unlike xcode-select --install (which won't fix the include directories on an upgrade) or ln -s ... /usr/include (which is now forbidden by System Integrity Protection).
I had the same problem and used the following youtube video to fix it.
https://www.youtube.com/watch?v=hrPm7tWC-BI&feature=youtu.be
or you can follow this path. Make sure to include the quotation marks
Project - Build Settings - Search Paths - Headers Search Paths, and add the following path:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/"
So, I restarted my laptop and everything seems to be fine right now, thanks for those who tried to help.
libstdc++ is not OK for Xcode Build & Compile time,
libstdc++ is OK for iPhone Run Time
From answer recommended by #Alan Birtles
libstdc++ Support was removed from the iOS 12.0 Simulator runtime, but
it remains in the iOS 12.0 (device) runtime for binary compatibility
with shipping apps.
I encountered this when declaration in .hpp file.
#include <iostream>
#include <string>
OK with
#ifdef __cplusplus
#include <iostream>
#include <string>
// usage code
#endif
I tried a fresh Catalina install with Xcode. I copied and pasted your code into "test.cpp" and then ran:
clang++ test.cpp
in the same directory as the "test.cpp" file from Terminal. The result was an "a.out" file which when run:
./a.out
output the required "Hello, World!" result. Hopefully that is of some use (as a point of reference).

C++ Boost - __assert_fail was not declared in this scope

Currently I am developing library that will be used on Raspberry Pi 3. To loop through all files in a directory I decided to use boost::filesystem module. I have prepared this code:
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp>
boost::filesystem::path targetDir(some_path);
boost::filesystem::directory_iterator it(targetDir), eod;
BOOST_FOREACH(boost::filesystem::path const& p, std::make_pair(it, eod))
{
if (boost::filesystem::is_regular_file(p))
{
// do smth with file
}
}
Boost has been installed through apt-get install libboost1.62-all-dev.
Unfortunately compiling in Visual Studio 2017 (on remote RPi target, g++ 4.9.2) gives me this bunch of errors:
__assert_fail was not declared in this scope (path_trails.hpp)
...
__assert_fail was not declared in this scope (path.hpp)
...
__assert_fail was not declared in this scope (shared_ptr.hpp)
What am I doing wrong?
The issue was not connected with boost. I had #include <Magick++.h> line above and this causes these output errors. After commenting this line everything runs fine, now I need to find why Magick++ is causing this compability issue.
I just ran into what seems like the same issue.
What is strange is I did not see this issue in stretch or buster, only in bullseye. Were you using a version of imagemagick that did not come from the Raspbian archive?
In any case the issue in my case was that Magick++.h ends up include assert.h inside a namespace, when this happens it breaks any other code that tries to use assert.h
The workaround is to re-order your includes and/or add an explicit include of assert.h so that assert.h gets included before Magick++.h

stoi was not declared in this scope, despite using C++11 [duplicate]

#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
I tried this code on my computer which is running MinGW GCC 4.7.2. It gives me this error:
What am I doing wrong, I got this from cppreference. Its the exact same code. And its a different error from the one described here.
It seems your MinGW needs a patch: Enabling string conversion functions in MinGW
This patch enables the following list of C++11 functions and templates
in the std namespace:
stoi, stol, stoul, stoll, stof, stod, stold,
to_string, to_wstring
In above link, there is a .zip file, download it and
Copy wchar.h and stdio.h from the include directory in the zip file
to the following directory (overwrite): C:\mingw\include (replace
C:\mingw\ with the appropriate directory)
Copy os_defines.h to the following directory (overwrite):
C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace
C:\mingw\ with the appropriate directory) (replace 4.7.0 with the
correct version number)
Another solution is to use MinGW-w64, which works correctly out of the box. This is a fork of MinGW that can produce both 32-bit and 64-bit builds.

'stoi' was not declared in this scope [C++] [duplicate]

#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
I tried this code on my computer which is running MinGW GCC 4.7.2. It gives me this error:
What am I doing wrong, I got this from cppreference. Its the exact same code. And its a different error from the one described here.
It seems your MinGW needs a patch: Enabling string conversion functions in MinGW
This patch enables the following list of C++11 functions and templates
in the std namespace:
stoi, stol, stoul, stoll, stof, stod, stold,
to_string, to_wstring
In above link, there is a .zip file, download it and
Copy wchar.h and stdio.h from the include directory in the zip file
to the following directory (overwrite): C:\mingw\include (replace
C:\mingw\ with the appropriate directory)
Copy os_defines.h to the following directory (overwrite):
C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace
C:\mingw\ with the appropriate directory) (replace 4.7.0 with the
correct version number)
Another solution is to use MinGW-w64, which works correctly out of the box. This is a fork of MinGW that can produce both 32-bit and 64-bit builds.

Problems with std::stoi, not working on MinGW GCC 4.7.2

#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
I tried this code on my computer which is running MinGW GCC 4.7.2. It gives me this error:
What am I doing wrong, I got this from cppreference. Its the exact same code. And its a different error from the one described here.
It seems your MinGW needs a patch: Enabling string conversion functions in MinGW
This patch enables the following list of C++11 functions and templates
in the std namespace:
stoi, stol, stoul, stoll, stof, stod, stold,
to_string, to_wstring
In above link, there is a .zip file, download it and
Copy wchar.h and stdio.h from the include directory in the zip file
to the following directory (overwrite): C:\mingw\include (replace
C:\mingw\ with the appropriate directory)
Copy os_defines.h to the following directory (overwrite):
C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace
C:\mingw\ with the appropriate directory) (replace 4.7.0 with the
correct version number)
Another solution is to use MinGW-w64, which works correctly out of the box. This is a fork of MinGW that can produce both 32-bit and 64-bit builds.