#include <fstream>
using namespace std;

void a(ifstream b){}
void main()
{
    ifstream b("c.txt");
    a(b);                      // error caused when function a() is used
}

Why is this producing an error?

Thank you,
Thumb2

Recommended Answers

All 5 Replies

Why do you have an extra set of parentheses on the end of your function prototype? Get rid of those and put a semicolon at the end.

Also, you probably want to pass in ifstream by reference:
void a(ifstream & b) in your prototype, nothing extra required in the function call itself.

commented: Straight to the solution +0

>Why is this producing an error?
Stream objects aren't copyable at the object level. As jonsca said, you need to pass a reference to the ifstream.

Also, I forgot to say in my first post that the ampersand should be included in your function definition as well.

Why do you have an extra set of parentheses on the end of your function prototype? Get rid of those and put a semicolon at the end.

Is it preferred that I use prototypes, even in simplified programs?

I was trying to make it easier to look at, for example:

//headers

void a(ifstream &b){}   // do nothing
void main()
{
    //code
}

versus the prototyped version:

//headers

void a(ifstream &);
void main()
{
    //code
}
void a(ifstream &b){}  // do nothing

//*************************
Passing by reference did solve it; thank you very much for the quick reply!

For a small scale project (i.e. 1 file) you can certainly get away with popping your function definition in its entirety before main(). When dealing with library functions, prototypes are essential because you don't have direct access to the original code for the most part.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.