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.
The below is my attempt:
#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; } 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?
ReadFileshould returnstd::istream &- andiStream.clear()rather thaniStream.setstate(std::ios::goodbit)