std::to_string not available in header string [duplicate] - c++

This question already has answers here:
to_string is not a member of std, says g++ (mingw)
(13 answers)
Closed 8 years ago.
I am trying to use std::to_string(), of course with #include <string>, but the compiler gives me the error that it was not declared in the scope, after lots of searching I have tried the fixes of going to compiler settings and have g++ follow... -std=c++11,
downloading 4.7 patch header files for wchar.h, stdio.h, os_defines.h
and re downloading the latest codeblocks version.
but nothing makes it seem to work.
what settings do I need to change to make this work.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s=to_string(10);
cout <<s<<endl;
return 0;
}
update: I have re-installed codeblocks,version 13.12 tmd gcc, with gcc version 4.81(tmd-2), I have both -std=c++0 and -std=c++11 flags on in compiler settings, in Toolchain executables i have c++ compiiler:mingw32-gcc-4.8.1.exe, and it Still does not recognize to_string();

I suppose you are using the Code::Blocks MinGW distribution which includes GCC 4.7.1. The MinGW implementation of libstdc++ does not support to_string, this is a known bug which cannot be fixed by simply patching the header files. Upgrade to the latest Code::Blocks version with GCC 4.8.1.

Related

Code calling reverse function does not compile on either g++ or clang++ on Ubuntu 18, but mysteriously works on mac osx

On Mac OSX, clang version 7.0.2 the code compiles.
On Ubuntu clang version 7.0.0 it does not. Is there really a difference in some default library, this seems weird?
MWE:
#include <string>
using namespace std;
int main() {
string A = "abc";
reverse(A.begin(), A.end());
}
Is one really bringing in algorithm somehow, but different between the two versions?
Yes default libraries vary on different systems with different compilers. If you use a certain function, include the respective header. On your Mac the reverse function seems to be include somewhere deep in the string header.
Use #include <algorithm> and it should work on the other systems too.
The default standard library on Mac OS is libc++.
The default standard library on Ubuntu is libstdc++.
You can try on Ubuntu by passing -stdlib=libc++ to the compiler, and see what happens.
The difference is (I suspect, but do not know for sure) that on libc++ string::iterator is a type in namespace std, so ADL lookup occurs, but in libstdc++ the iterators are just char *, and since they don't live in namespace std, no lookup in that namespace occurs.

Compiler can't find 'aligned_alloc' function

I'm trying to launch the example code from aligned alloc:
#include <cstdio>
#include <cstdlib>
int main()
{
int* p1 = static_cast<int*>(std::malloc(10*sizeof *p1));
std::printf("default-aligned address: %p\n", static_cast<void*>(p1));
std::free(p1);
int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
std::printf("1024-byte aligned address: %p\n", static_cast<void*>(p2));
std::free(p2);
}
My compilers give me this error:
$ g++-mp-8 main.cpp -std=c++17
main.cpp:10:38: error: no member named 'aligned_alloc' in namespace 'std'
int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
I am working with macOS High Sierra 10.13.6 and tried to compile this code with Macport's GCC 7.3.0, 8.2.0 and CLang (Apple LLVM version 10.0.0), they all produce the same error.
Edit: It doesn't work with either std:: present or not.
Edit2: I installed macOS Mojave and that did not fix the problem. I hoped it'll reinstall macOS's toolchain but it didn't. So I guess I cannot accept provided answers until I get a more specific one.
I am not using macOS but I have similar problems on linux using a custom g++. If you look at the cstdlib header, there is something like
#if __cplusplus >= 201703L && defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
using ::aligned_alloc;
#endif
So aligned_alloc is only pulled into the std namespace if C++17 is available and glibcxx supports it. You can check x86_64-linux-gnu/bits/c++config.h (or something similar on macOS) if _GLIBCXX_HAVE_ALIGNED_ALLOCis defined. If not your glibc version is too old.
For clang and the libc++ implementation aligned_alloc is available if _LIBCPP_HAS_C11_FEATURES is defined which again depends on a recent version of glibc.
As an alternative you can use boost.
As the accepted answer mentions it, using boost::align::aligned_alloc solves the problem.
To fix the error without source modification, just add the following on top of the file:
#ifdef __APPLE__
#include <boost/align/aligned_alloc.hpp>
using boost::alignment::aligned_alloc;
#endif

eclipse build error with regex

I'm trying to implement regex in my parser program when i used codeblocks all works fine, on eclipse neon im having Function 'regex_match' could not be resolved error. I added flag to gcc c++ compiler settings -std=c++11 and checked CDT GCC Built-in compiler settings MiniGW.
How to make it work on eclipse ?
#include "ParserSip.h"
#include <regex> //its included ok
void checkIfCseq (SipMessage & pointer, string stringLine){
string stringToFind = ".*CSeq:.*";
regex_match(); // Function 'regex_match' could not be resolved
}

error: 'stoi' was not declared in this scope (Code::Blocks 16.01 on Windows 10) [duplicate]

This question already has answers here:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
(4 answers)
Closed 6 years ago.
I get the error: 'stoi' was not declared in this scope.
#include <string>
using namespace std;
...
int x;
x = stoi(arg[0]);
I am running Code::Blocks 16.01 on Windows 10 with the -std=c++11 setting.
I didn't find any useful information on this page:
‘stoi’ was not declared in this scope
I saw somewhere that upgrading gcc can fix this problem, but I didn't find an appropriate installation on the page: https://mingw-w64.org/doku.php/download
The same code works fine in Code::Blocks 13.12 on Linux Mint 17.3.
Is there a recommended fix for this problem? Can this be fixed by using the 64 bit version of MinGW (assuming that is compatible with Code::Blocks 16.01)?
Update
there is a workaround, using atoi and c_str instead:
x = atoi(arg[0].c_str());
I believe that this is a bug with MinGW. For more information, check out this StackOverflow post. Specifically, DRH's answer.

std::stod is not a member of std [duplicate]

This question already has answers here:
Problems with std::stoi, not working on MinGW GCC 4.7.2
(2 answers)
Closed 6 years ago.
I can't compile the following code
auto test = 42.02;
double right = std::stod(stck.top());
I'm using Code::Blocks and activated the build option to follow c++11 standard. The compiler does not complain about the auto declaration and compiles, when I put the line below in comments.
I included the string header. I'm not using a namespace.
I have no idea why this does not compile. Please help me!
edits:
My compiler is Standard MinGW GCC 4.9
For simplicity reasons, I tried the following: compiled with -std=c++11
#include <string>
int main(){
double pi = std::stod("3.14");
return 0;
}
I get the following error:
error: stod is not a member of std.
std::stod is only available if you are at least using std=c++11 to compile. Therefore, when you compile, just add the flag -std=c++11 and you will be able to use stod
Seems like you've most likely misspelled std::strtod()
You'll also need to
#include <cstdlib>