Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to write a function with an ifstream& argument.

void word_transform(ifstream & infile)
    infile("content.txt");
    //etc

which gave me an error:

Type 'ifstream' (aka 'basic_ifstream ') does not provide a call operator.

Can you please me what's wrong?

Functions that take stream arguments usually work with a stream that is already open. For example, ifstream s("content.txt"); word_transform(s);. – molbdnilo Dec 16, 2014 at 9:23

call operator is a function like operator()( params ) allowing to use the syntax myObject( params ).

So, when you write infile(...), you are trying to us a call operator.

What you are trying to do is to open a file, use the open method:

void word_transform(ifstream & infile)
    infile.open("content.txt",std::ios_base::in);
    if ( infile.is_open() )
        infile << "hello";
    infile.close();

But, as commented, it does not really make sense to pass infile reference to such a function. You may consider:

void word_transform(istream& infile)
    infile << "hello";
int main()
    ifstream infile;
    infile.open("content.txt",std::ios_base::in);
    if ( infile.is_open() )
        word_transform( infile );
    infile.close();
    return 0;
void word_transform()
    ifstream infile;
    infile.open("content.txt",std::ios_base::in);
    if ( infile.is_open() )
        infile << "hello";
    infile.close();
int main()
    word_transform();
    return 0;
                Your last comment is very pertinent: if he is going to open and close the file in the function, the std::ifstream should be local to the function; it serves no purpose to have it as an argument.  A more frequent idiom, however, would be to have it opened and closed by the caller, and to pass a std::istream& argument (rather than an std::ifstream&).
– James Kanze
                Dec 16, 2014 at 10:44

You attempt to call operator() on your parameter. That will not work. Are you trying to open a file? If you get an ifstream as parameter, it should be open from the start because you opened it outside your function. Passing a stream and then opening it inside your function does not make sense.

void word_transform(std::ifstream& infile)
    // read something from infile
int main()
   std::ifstream file("content.txt");
   // error checks
   word_transform(file);
   return 0;

Note that this would try to call operator() on already created object of type infile. As no such operator exists from ifstream , you got an error.

Rather you should do:-

infile.open("content.txt");

Typically, (references to) streams are passed to functions for I/O. This means the function should take std::istream or std::ostream argument, but not std::ifstream or std::ofstream. This way your function can be used with any stream object derived from std::istream, including std::cin, std::istrstream, and std::ifstream.

As nvoigt said, passing an std::ifstream to a function for opening makes little sense. It is definitely not clear to the caller that its stream may be closed and opened to another file.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.