0
#include <iostream>
#include <fstream>

void ReadFile(ifstream in);

int main() {
    ifstream in("handout.txt");
    ReadFile(in);
    return 0;

}

void ReadFile(ifstream  in){
    while(true){
        string word;
        in >> word;
        if(in.fail()) break;
        cout << word << endl;
    }
}

I am new to C++ trying to explore. Just by removing & in from function definition and declaration void ReadFile(ifstream & in) which works fine. I get a series of errors I am trying to understand.

C:\Users\samru\Downloads\simple-project\simple-project\src\average.cpp:39: error: use of deleted function 'std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)'
     ReadFile(in);
                ^
c:\qt\qt5.1.1\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\fstream:427: 'std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)' is implicitly deleted because the default definition would be ill-formed:
     class basic_ifstream : public basic_istream<_CharT, _Traits>
           ^
c:\qt\qt5.1.1\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\fstream:427: error: use of deleted function 'std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)'
unknown29
  • 47
  • 5
  • 1
    `ifstream` is not copyable. – Raymond Chen Jan 06 '21 at 14:33
  • You're missing a whole lot of `std::`'s. You also *cannot* pass an `ifstream` by value, as they have a deleted copy constructor. Does this answer your question? [Why do I make "use of deleted" function when passing a std::ofstream as parameter?](https://stackoverflow.com/questions/31674353/why-do-i-make-use-of-deleted-function-when-passing-a-stdofstream-as-paramete) – scohe001 Jan 06 '21 at 14:33

1 Answers1

0

By removing the &, you are now making the ReadFile function use a copy of the fstream rather than a reference to it which it can not do.

lostbard
  • 5,065
  • 1
  • 15
  • 17