Why is my fstream implicitly deleted?

I work with several HID devices, all of which have classes based on the following base class (in main.h):

class HIDDevice { public: hid_device *device; virtual void read(std::fstream)=0; virtual void write(std::fstream)=0; }; 

Here is one of the device classes derived from it (device.h):

 class MyDevice : public HIDDevice { public: void read(std::fstream); void write(std::fstream); }; 

... and implementation example:

 void MyDevice::read(std::fstream file) { // Read from card and write to file response = send_command(READ_DEVICE); file.write((char *)&response[0], response.size()); } 

... and the caller:

 fstream file (filename, ios::binary | ios::in); dev->read(file); 

When I try to compile, I get the following error:

main.cpp: 294: 27: error: using the remote function 'std :: basic_fstream :: basic_fstream (const std :: basic_fstream &)
In the file included from source / main.cpp: 24: 0: / usr / include / c ++ / 4.6 / fstream: 761: 11: error: 'std :: basic_fstream :: basic_fstream (const std :: basic_fstream &) implicitly deleted as the default definition will be poorly formed:

... and I have no idea why, probably because I'm fairly new to C ++ and I did something stupid.

By changing the arguments to the links (using &), I get the following error:

/main.o :(. rodata._ZTV13MyDevice [vtable for MyDevice] + 0x18): undefined reference to `MyDevice :: write (std :: basic_fstream> &) '

Can someone help me fix this problem?

+8
c ++ inheritance class fstream
source share
4 answers

Try throwing those std::fstream into the link.

 class MyDevice : public HIDDevice { public: void read(std::fstream&); void write(std::fstream&); }; 
+17
source share

Just for reference, I had the same compiler error, but created it differently, which was not immediately obvious to me. Out of habit, I wrote:

 auto stream = fstream(output, iOS::out | iOS::binary); 

Which of course creates a temporary fstream object and copies it to the stream. This worked in Xcode 6 using clang, but not for GCC 4.9.2.

+4
source share

HIDDevice signatures mean that fstream are passed by value. This, in turn, means that copies of the source stream objects must be created on the call site using the copy constructor. It is this copy constructor that has the canonical signature std::basic_fstream::basic_fstream(const std::basic_fstream&) .

The compiler tells you that for some reason (which is related to the features of the basic_fstream implementation used by your standard library), it cannot automatically generate this copy constructor, therefore, it will not be able to execute your implicit command to make copies of the streams.

As others have said, the usual modus operandi (which will also prevent the error here) is to pass arguments by reference instead of values.

+2
source share

&std::fstream as a parameter will pass it by reference, which will be used as the same object throughout the world. This avoids the implementation by trying to create another instance, and then close the stream when the functions complete (since the copy object is destroyed).

Make sure you change this both in the declaration and in the implementation of the member function (i.e. in the class and where the body is located).

+1
source share

All Articles