8

I have a member that is std::ofstream fBinaryFile and a

void setFile( std::ofstream& pBinaryFile ) 
{
    fBinaryFile = pBinaryFile;
}

output:

 Data.h:86:16: error: use of deleted function ‘std::basic_ofstream<char>& std::basic_ofstream<char>::operator=(const
 std::basic_ofstream<char>&)’
     fBinaryFile = pBinaryFile;
                 ^

I understood that copy in std::ofstream is not allowed and maybe I'm missing something. Is possible save the content of pBinaryFile in fBinaryfile?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
user3050386
  • 139
  • 1
  • 9

2 Answers2

12

Because the relevant operator is declared as

ofstream& operator= (const ofstream&) = delete;

which means it is explicitly prohibited so ofstream semantics does to support copying.

Depending on your architecture you can store a pointer/reference or move it.

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Alexander Balabin
  • 2,055
  • 11
  • 13
  • in the documentation: move: ofstream& operator= (ofstream&& rhs); I can't figure out , still not working. I understand the problem but I can't understand how to move pBinaryFile in fBinaryFile. Move assignment Acquires the contents of rhs, by move-assigning its members and base classes. – user3050386 Jul 28 '15 at 13:21
  • Use `std::move` as in `fBinaryFile = std::move(pBinaryFile)`. However make sure you read on move operations and understand what is going on when you do that. – Alexander Balabin Jul 28 '15 at 13:27
  • Thank you very much for the help, but this didn't fixed the problem, I read the documentation and I can-t understand what I'm missing, I hope is not a compilation problem, I use g++ -std=c++11 ....... – user3050386 Jul 28 '15 at 14:27
  • sorry, what problem are you referring to? – Alexander Balabin Jul 28 '15 at 14:37
  • error: use of deleted function, I used the function void setFile( std::ofstream& pBinaryFile ) { fBinaryFile = std::move(pBinaryFile); } but error is not solved, around in internet I saw that g++ has some problem with move function. – user3050386 Jul 28 '15 at 15:00
  • 1
    what compiler are you using? I just tried in gcc 5.2 it is ok, 4.9.2 fails because its version of stl indeed does not have the move operator= – Alexander Balabin Jul 28 '15 at 15:30
  • I use gcc 4.8.2 on scientific linux, maybe this is the problem, I try to update, thanks for the help – user3050386 Jul 29 '15 at 08:10
  • @AlexanderBalabin thank you so much. switching from gcc 4.8 to 5 resolved the `use of delete` error from compiler. – pangyuteng May 15 '21 at 02:02
0

If you want to copy the content of pBinaryFile to fBinaryFile you need to declare pBinaryfile as an ifstream (input file stream), not an ofstream (output file stream) It should look something like this:

std::ifstream pBinaryFile;
std::ofstream fBinaryFile;
std::stringstream sstream;
std::string line
pBinaryFile.open(pBinaryFileName.c_str());
fBinaryFile.open(fBinaryFileName.c_str());
if (pBinaryFile.isopen()) {
    while (pBinaryFile.good()) {
        getline(pBinaryFile, line);
        fBinaryFile << sstream(line) << endl;
    }
}
pBinaryFile.close();
fBinaryFile.close();

Note that pBinaryFileName and fBinaryFileName refer to the paths of your files.

There might be mistakes in this code, but I think the solution looks similar to this.

I suggest this for further reading:

http://www.cplusplus.com/doc/tutorial/files/

benhal
  • 33
  • 4