-1

Can someone tell me what is wrong in this code? Particularly the function longestLine.

When I run the code without that funcion (only using the inside of it) the program runs with no problems, but when I do it with the function it does not compile.

I dont understand the error the compiler gives but I think it has something to do with the argument of the funcion.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string longestLine(ifstream infile);
string promptUserForFile(ifstream & infile, string prompt="");

int main() {
    ifstream infile;
    promptUserForFile(infile);
    cout << "The longest line of the file is: " << endl;
    cout << longestLine(infile);
    return 0;
}

string promptUserForFile(ifstream & infile, string prompt) {
    while (true) {
        cout << prompt;
        string filename;
        getline(cin, filename);
        infile.open(filename.c_str());
        if (!infile.fail()) 
            return filename;
        infile.clear();
        cout << "Unable to open that file. Try again." << endl;
        if (prompt == "") 
            prompt = "Input file: ";
    }
}

string longestLine(ifstream infile) {
    int length = 0;
    string longest_line;
    string line;
    while (getline(infile, line)) {
        if (line.length() > length) {
            length = line.length();
            longest_line=line;
        }
    }
    return longest_line;    
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 2
    Pass `infile` to `longestLine` by reference. You are currently passing it by value and a stream cannot be copied, so... – Paul Sanders May 27 '22 at 23:25
  • @PaulSanders But I am not trying to modify the stream, I am just reading what is inside. Why do I need to pass it by reference? – ethan davitt May 27 '22 at 23:27
  • 2
    Because streams cannot be copied (and passing by value implies a copy). – Paul Sanders May 27 '22 at 23:27
  • 1
    See [this](https://stackoverflow.com/questions/36051672/why-are-iostreams-not-copyable). – Beta May 27 '22 at 23:28
  • 6
    *"But I am not trying to modify the stream"*. You are: `std::getline(infile, line)` modifies the stream. – Jarod42 May 27 '22 at 23:28
  • 3
    ... because, at minimum, it updates the current position, and maybe, on any particular call, also refills the underlying buffer. – Paul Sanders May 27 '22 at 23:32
  • 1
    What is the error you get after fixing it? – Nathan Pierson May 27 '22 at 23:39
  • 1
    Compile fine with reference (changed at 2 places, (also change one `int` to `std::size_t` to avoid warning)) [Demo](https://godbolt.org/z/6Y7xa31M7). – Jarod42 May 27 '22 at 23:41
  • *"Particularly the function longestLine."* -- why include more than that function in your [mre]? We don't need your full program; we only need enough to reproduce the problem. (For an error when compiling, it's likely that you can remove all other function definitions from the example. Maybe a declaration for another function, but not the definition.) – JaMiT May 27 '22 at 23:50
  • Please include the error message (copied as text) in your question so that others with the same problem can find your question with a search. (Speaking of which, did you try searching this site for the error message? That tends to yield faster answers than asking a new question.) – JaMiT May 27 '22 at 23:52

1 Answers1

0

I think you should pass ifstream by reference

string longestLine(ifstream& infile);

ifstream derive from ios_base, the copy constructor of stream is deleted: because streams are not copyable.

If you pass ifstream by value, the compiler will try to call the copy constructor of ifstream when you call longestLine, the compiler will definitely complain this error.

enter image description here

ramsay
  • 3,197
  • 2
  • 14
  • 13