Function cannot be referenced as it is a deleted function

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.

Leave a Comment