0

I have written a C++ class which includes an ifstream as a member. However, when compiling the class, the following compilation error comes up:

Error   1   error C2248: 'std::basic_ifstream<_Elem,_Traits>::operator =' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>' c:\path\to\project\parser\bitstreamreader.h 55

The error is said to occur on the last line, where the class declaration is terminated. I understand that streams cannot be copied, hence that the = operator is inaccessible. However, as you can see from the code below, all I am doing here is declaring the existence of an ifstream. Why should that trigger this error at all?

class BitstreamReader {
public:
    BitstreamReader(std::string file_name);
    BitstreamReader();
    ~BitstreamReader();
    UINT8                   ReadBit();
    UINT32                  ReadBits(UINT32 bits);
    INT32                   ReadSignedBits(UINT32 bits);

    UINT8                   ReadByte();

    UINT32                  NextBits(UINT32 bits);
    UINT32                  ByteAlignedNextBits(UINT32 bits);

    UINT32                  GetPerByteBitOffset();

    BOOL                    ByteAligned();
    VOID                    AlignToByte();

    BOOL                    EndOfBitstream();

    VOID                    OpenFile(std::string file_name);

    std::ifstream               BitstreamFile;

private:

    VOID                    FillBuffer();

    UINT32                  BitstreamPointer;
    UINT32                  BitBuffer;
    UINT8                   BitBufferPointer;
    UINT8                   BitBufferSize;
};

I had at first declared the ifstream as a private member of this BistreamReader class. However, when the error occured I tried making it public to see if it made any difference. It did not.

This error is really vague. I have no idea what's causing it. I am not trying to use the = operator in any of the method implementations, either..

Here are the constructors (after trying out some of the solutions in other questions):

BitstreamReader::BitstreamReader() : 
    BitstreamFile("empty.dat", std::ios::in)
{
    BitstreamPointer = 0;
    FillBuffer();
    BitBufferSize = sizeof(BitBuffer) * CHAR_BIT;
}

BitstreamReader::BitstreamReader(std::string file_name) : 
    BitstreamFile(file_name, std::ios::binary)
{
    BitstreamPointer = 0;
    FillBuffer();
    BitBufferSize = sizeof(BitBuffer) * CHAR_BIT;
}
Luke
  • 2,434
  • 9
  • 39
  • 64
  • Can you post your constructor implementations? – TartanLlama Jun 02 '15 at 09:51
  • @m.s. none of those solutions work for me. – Luke Jun 02 '15 at 10:54
  • @TartanLlama I have added constructor implemenations – Luke Jun 02 '15 at 10:56
  • 1
    You are, at some point, assigning to a `BitstreamReader`. Since you haven't provided an assignment operator, the default method is used, which is "member-wise" assignment. That fails because you can't copy an `std::ifstream`. – molbdnilo Jun 02 '15 at 10:59
  • Yes, in some place you copy `BitstreamReader` object. Please, provide code how do you use `BitstreamReader`. – gomons Jun 02 '15 at 11:01

0 Answers0