fstream - cannot access private members declared in class basic_fstream<_Elem, _Traits>



I use fstream to open a file for writing purpose.

1
2
3
fstream streamFile;  
streamFile.open ( "C:\\path\\to\\textFile.txt", 
fstream::in | fstream::out| fstream::app);


I get the following error:

cannot access private members declared in class basic_fstream<_Elem, _Traits>

What is missing?
What is missing?
Some sort of context, such as the rest of the code, which compiler is used, the line number where the error occurs, etc.

This code compiles without error in two different compilers, including TDM-GCC 4.7.1 32-bit.

1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>

using std::fstream;

int main()
{
    fstream streamFile;
    streamFile.open ( "C:\\path\\to\\textFile.txt",
        fstream::in | fstream::out| fstream::app);

    return 0;
}


Edit
It looks as through an error like this may occur when trying to make a copy of a stream. (But that isn't indicated in the above code fragment, perhaps it's elsewhere in the program?)
Last edited on
Thanks for the hint:

Such a method should be the trigger as the fstream has to be sent as fstream&, right?!

1
2
3
void TextFile::setMyWinFile(std::fstream p_myWinFile) {
    myWinFile = p_myWinFile;
}




You'd pass the fstream as a reference or pointer. In this case a pointer seems what is appropriate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>

using std::fstream;

class TextFile {
public:
    void setMyWinFile(std::fstream * p_myWinFile);
    
private:
    std::fstream * myWinFile;    
    
};

void TextFile::setMyWinFile(std::fstream * p_myWinFile) {
    myWinFile = p_myWinFile;
}


int main()
{
    fstream streamFile;
    streamFile.open ( "C:\\path\\to\\textFile.txt",
        fstream::in | fstream::out| fstream::app);
    
    TextFile txt;
    txt.setMyWinFile(&streamFile);
    
   
    return 0;
}
Thanks for the detailed reply!
The data-member of class is not a stream *. It is fstream.
How should I change my code, then?
Well, the root of the error is that a stream may not be copied. If you want the class member to be a stream rather than a pointer, don't attempt to assign to it. Instead, do all open/close etc. directly with that member.
Topic archived. No new replies allowed.