Okay, so I have
tmp.cpp:
#include <string>
int main()
{
std::to_string(0);
return 0;
}
But when I try to compile I get:
$ g++ tmp.cpp -o tmp
tmp.cpp: In function ‘int main()’:
tmp.cpp:5:5: error: ‘to_string’ is not a member of ‘std’
std::to_string(0);
^
I'm running g++ version 4.8.1. Unlike all the other references to this error that I found out there, I am not using MinGW, I'm on Linux (3.11.2).
Any ideas why this is happening? Is this standard behaviour and I did something wrong or is there a bug somewhere?
you may want to specify the C++ version with
g++ -std=c++11 tmp.cpp -o tmp
I don't have gcc 4.8.1 at hand , but in older versions of GCC,
you can use
g++ -std=c++0x tmp.cpp -o tmp
At least gcc 4.9.2 I believe also support part of C++14 by specifying
g++ -std=c++1y tmp.cpp -o tmp
Update:
gcc 5.3.0 (I am using the cygwin version) supports both -std=c++14 and -std=c++17 now.
to_string works with the latest C++ versions like version 11. For older versions you can try using this function
#include <string>
#include <sstream>
template <typename T>
std::string ToString(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
By adding a template you can use any data type too.
You have to include #include<sstream> here.
Related
I am trying to compile this code with MinGW g++ (i686-win32-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int, int> mmap;
mmap[0]=10;
mmap[1]=20;
mmap[2]=30;
mmap[3]=40;
mmap[4]=50;
for(auto [x,y]:mmap){
cout<<x<<"->"<<y<<endl;
}
return 0;
}
Compiling with c++11 flag gives this
E:\Code>g++ temp.cpp -std=c++11
temp.cpp: In function 'int main()':
temp.cpp:89:14: warning: structured bindings only available with -std=c++17 or -std=gnu++17
for(auto [x,y]:mmap){
and compiling with c++17 flag gives lines and lines of errors.
g++ temp.cpp -std=c++17
OK, so I figured this out and this came out to be the very first line
#include <bits/stdc++.h>
Including iostream and map instead of the above line results in a clean compilation.
#include<iostream>
#include<map>
Now I have one more reason as to Why should I not #include <bits/stdc++.h>?
I have some problems with building my c++ app with make.
I have such c++ code:
#include <string>
#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main()
{
auto iter = fs::directory_iterator(fs::current_path());
while(iter._At_end())
{
cout << iter->path() << endl;
}
return 0;
}
I am trying to compile object file with command:
g++-9 -std=c++17 -Wall -Wextra -pthread -lstdc++fs -c -o main.o main.cpp
But I have this error:
main.cpp:12:16: error: ‘class std::filesystem::__cxx11::directory_iterator’ has no member named ‘_At_end’
12 | while(iter._At_end())
| ^~~~~~~
So, I cant use members of classes of std::filesystem namespace but if I wanna use only class(for example std::filesystem::path), so everything is ok.
Versions of soft:
g++-9 (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
GNU Make 4.2.1
Hope you'll help me.
Just don't use _At_end() and everything will be ok.
I've following program.
#include <iostream>
#include <string>
int main() {
int i {0};
std::string str {"Hello World"};
std::cout << i << " : " << str << std::endl;
return 0;
}
When I compile this with g++ I got following error.
I'm using g++ 5.4. g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
I want to know what is the standard way to compile program in g++ using std::C++14 with necessary flags.
Thanks in Advance.
Update:
I've done it with following: g++ ./ex01.cpp -o ex01.out -std=c++14 -Wall -o2
Compile with the flag:
-std=c++14
Okay, so I have
tmp.cpp:
#include <string>
int main()
{
std::to_string(0);
return 0;
}
But when I try to compile I get:
$ g++ tmp.cpp -o tmp
tmp.cpp: In function ‘int main()’:
tmp.cpp:5:5: error: ‘to_string’ is not a member of ‘std’
std::to_string(0);
^
I'm running g++ version 4.8.1. Unlike all the other references to this error that I found out there, I am not using MinGW, I'm on Linux (3.11.2).
Any ideas why this is happening? Is this standard behaviour and I did something wrong or is there a bug somewhere?
you may want to specify the C++ version with
g++ -std=c++11 tmp.cpp -o tmp
I don't have gcc 4.8.1 at hand , but in older versions of GCC,
you can use
g++ -std=c++0x tmp.cpp -o tmp
At least gcc 4.9.2 I believe also support part of C++14 by specifying
g++ -std=c++1y tmp.cpp -o tmp
Update:
gcc 5.3.0 (I am using the cygwin version) supports both -std=c++14 and -std=c++17 now.
to_string works with the latest C++ versions like version 11. For older versions you can try using this function
#include <string>
#include <sstream>
template <typename T>
std::string ToString(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
By adding a template you can use any data type too.
You have to include #include<sstream> here.
I compiled & installed gcc4.4 using macports.
When I try to compile using -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...:
#include <thread>
...
std::thread t(handle);
t.join();
....
The compiler returns:
cserver.cpp: In member function 'int CServer::run()':
cserver.cpp:48: error: 'thread' is not a member of 'std'
cserver.cpp:48: error: expected ';' before 't'
cserver.cpp:49: error: 't' was not declared in this scope
But std::cout <<... compiles fine..
Can anyone help me?
gcc does not fully support std::thread yet:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Use boost::thread in the meantime.
Edit
Although the following compiled and ran fine for me with gcc 4.4.3:
#include <thread>
#include <iostream>
struct F
{
void operator() () const
{
std::cout<<"Printing from another thread"<<std::endl;
}
};
int main()
{
F f;
std::thread t(f);
t.join();
return 0;
}
Compiled with
g++ -Wall -g -std=c++0x -pthread main.cpp
Output of a.out:
Printing from another thread
Can you provide the full code? Maybe there's some obscure issue lurking in those ...s?
I had the same issue on windows using MinGW. I found wrapper classes for in on github mingw-std-threads Including
mingw.mutex.h, mingw.thread.h files to global MinGW directory fixed this issue. All I had to do is to include header file and my code stayed the same
#include "mingw.thread.h"
...
std::thread t(handle);
...
Drop -ansi, it means -std=c++98, which you obviously don't want. It also causes macro __STRICT_ANSI__ to be defined and this may change the behavior of the headers, e.g. by disabling C++0x support.