Question

I am trying to declare an ifstream object in a header file as is shown but I get an error saying that it cannot be accessed. I have tried various things such as making it into a pointer instead, initialising in the .c file etc. but my code can't seem to get part the declaration of it.

ReadFile.h:

#ifndef READFILE_H
#define READFILE_H

#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <fstream>

class ReadFile{

private:
    std::ifstream stream;

public:
    std::string read();

    ReadFile();                                 // Default constructor
    ~ReadFile();                                    // Destructor
};

#endif

ReadFile.c: #include "ReadFile.h"

ReadFile::ReadFile(){
stream.open("./data.txt");
}

ReadFile::~ReadFile(){
stream.close();
}

The error that I am getting is:

Error   9   error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>' c:\users\Bob\documents\project\models\readfile.h    23  1   Project

The output is:

1>c:\users\Bob\documents\project\models\readfile.h(23): error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\fstream(827) : see declaration of 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'ReadFile::ReadFile(const ReadFile &)'

The error occurs when std::ifstream stream; is included and will disappear when this line is removed. What could be causing this error? Have I missed something really obvious or is there more to it?

Was it helpful?

Solution

The problem is that std::ifstream doesn't have a public copy constructor (because copying one wouldn't make sense) but the compiler-generated copy constructor for your class wants to use it.

It doesn't have any available assignment operator for the same reason (i.e. copying a std::ifstream is nonsense).

You should disallow copying and assignment for your class as well.

A simple way is to add

private:
    ReadFile(const ReadFile&);
    ReadFile& operator=(const ReadFile&);

to your class, if you're using C++03.

In C++11, use the = delete syntax.

public:
    ReadFile(const ReadFile&) = delete;
    ReadFile& operator=(const ReadFile&) = delete;

OTHER TIPS

This isn't an answer per se, but in case anyone comes across this question in the future after struggling as much as I just have, I decided to post it anyway.

I was facing a very similar issue to the OP for basically the same reason, although the error message that Visual C++ was giving me was 'ClassName::ClassName(const ClassName &)': attempting to reference a deleted function. In my inexperienced mind, I was thinking "WTF, I'm not copying or assigning any copies anywhere!".

However, after an hour of screaming at the computer in frustration I was able to narrow the problem down to the ifstream class member, which led me here. And thanks to the accepted answer, I was able to narrow the problem down even further to this single line of code:

list.emplace_back(objParam);

Little did I know that std::vector objects need a copy constructor defined on their template parameter's type in order to reserve space for future emplace_back() calls and the like!

So the solution comes from this SO answer courtesy of Bryan Chen (emphasis added by me):

So you can see emplace_back does use the desired constructor to create the element and call copy constructor when it need to grow the storage. You can call reserve with enough capacity upfront to avoid the need to call copy constructor.

Full credit and many thanks go to Bryan and molbdnilo for leading me to this discovery.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top