Ofstream does not work on Linux

I have a simple test code:

#include <string>
#include <iostream>
#include <fstream>

int main() {
std::ofstream strm = std::ofstream("test.txt");
strm << "TEST123";
strm.close();
return 0;
}

      

If I compile this on windows it works fine. however, when I compile it in debian with the following command: g ++ - 4.7 -std = c ++ 0x -lpthread TestStream.cpp -ldl -o TestStream than gives the following output: enter image description here

I've searched for this error to no avail. does anyone know how to fix this? I use a lot of threads in my projects and would like to compile it on Linux as well.

EDIT: so now I can compile it thanks to WinterMute, but now it prints empty files. How to fix it?

EDIT2: not sure why, but compiled it a second time. THH!

+3


source to share


2 answers


Using

std::ofstream strm("test.txt");

      

It:

std::ofstream strm = std::ofstream("test.txt");

      



a copy constructor is required that std::ofstream

does not have, or a move constructor is only available since C ++ 11. GCC 4.7 does not yet have full support for C ++ 11, and apparently this is one of the missing features.

In the comments, TC mentions that moving streams won't be coming to gcc until version 5, which is slated to be released this year. This took me by surprise because gcc declared full support for C ++ 11 with version 4.8.1, which is true for the compiler but not for libstdC ++. Reality bites.

So it's worth mentioning that libC ++ (the C ++ standard library implementation related to clang and llvm) implements movable streams, as well as clang 3.5 and gcc 4.9 (the ones I have here and tried) to compile from source if it is used instead of libstdc ++.

+11


source


std::ofstream strm = std::ofstream("test.txt");

      



ofstream

does not have a copy constructor.

0


source







All Articles