Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function cannot be referenced as it is a deleted function

Tags:

c++

Hello I am learning C++ from a book and am on a exercise question below

Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream.

#include "stdafx.h" #include <iostream> #include <istream> #include <string> #include <string.h> #include <list> #include <vector> #include <fstream>  std::istream ReadFile(std::istream &iStream) {     std::string word;     while (iStream >> word)     {}     std::cout << "I read value " << word << std::endl;     iStream.setstate(std::ios::goodbit);     return iStream;  }  int _tmain(int argc, _TCHAR* argv[]) {     ReadFile(std::cin);      system("pause");     return 0; } 

The above is my attempt, however I am getting errors at the "return iStream" line.

Error1 error C2280: 'std::basic_istream<char,std::char_traits<char>>::basic_istream(const std::basic_istream<char,std::char_traits<char>> &)' : attempting to reference a deleted function    2 IntelliSense: function "std::basic_istream<_Elem, _Traits>::basic_istream(const std::basic_istream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 77 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\istream") cannot be referenced -- it is a deleted function 

I don't really know what these errors are, I am aware you can delete stuff but I am not onto that topic in the book yet. As far as I know I have not at all touched the istream file... Can someone help me please?

Thanks!

like image 832
Foysal94 Avatar asked Sep 13 '14 21:09

Foysal94


People also ask

Is a deleted function?

A deleted function is a special function (constructor, destructor, operator) that has been explicitly disabled. If you look carefully at the error you can see that the function is the basic_istream copy-constructor, which is disabled because istreams cannot be copied.

What is function delete in C++?

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.

What is the use of Delete function?

Use the DELETE function to erase the data contents of a specified field, value, or subvalue and its corresponding delimiter from a dynamic array. The DELETE function returns the contents of the dynamic array with the specified data removed without changing the actual value of the dynamic array.


2 Answers

You can’t return an istream by value because it’s not copyable.

Since it’s not copyable the copy constructor has been deleted (to enforce the non-copyability), and that’s the direct technical cause of the diagnostic.

So, instead of

std::istream ReadFile(std::istream &iStream) 

… do

std::istream& ReadFile(std::istream& iStream) 

In other news, …


Instead of

#include "stdafx.h" 

just turn off precompiled headers in the Visual Studio project settings.

This also gives you more standard-conforming behavior for header inclusions.

If you don’t do that, then configure the project so that any warning about skipping an include, yields a hard compilation error.


Instead of

iStream.setstate(std::ios::goodbit); 

… do

istream.clear(); 

Instead of the non-portable Microsoft monstrosity

int _tmain(int argc, _TCHAR* argv[]) 

just use standard

int main() 

or in C++11 trailing return type syntax,

auto main() -> int 

Instead of

system("pause"); 

simply run your program via Ctrl+F5 in Visual Studio. Or, place a breakpoint on the last right brace of main and run in the debugger. Or, run the program from the command line.


The exercise formulation

should read the stream until it hits end-of-file

is ambiguous, but anyway reading words, as you’re doing, does not faithfully reproduce whitespace in the stream. For a more accurate reproduction of the stream contents you can either read character by character, or (via getline) line by line. Or, you can use a special mechanism for this task, namely outputting the read buffer, which does everything in one little statement.


Finally, you don’t need all those headers. You only need <iostream>, and if you choose to read lines, also <string>. Also, you don’t need the return 0; at the end of main, because that’s the default.

like image 148
Cheers and hth. - Alf Avatar answered Sep 16 '22 23:09

Cheers and hth. - Alf


A deleted function is a special function (constructor, destructor, operator) that has been explicitly disabled. If you look carefully at the error you can see that the function is the basic_istream copy-constructor, which is disabled because istreams cannot be copied. You are attempting to copy the istream when you return istream, since your function is declared as returning an istream (rather than e.g. returning a reference to an istream).

like image 27
nneonneo Avatar answered Sep 17 '22 23:09

nneonneo