error C2248: cannot access private member declared in class

Ok So i have a program that takes input from a file and tells whether the number is even or odd. Im getting this error in my code though and am confused as to what is the problem. Any help on this subject would be great.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>		// For cin / cout
#include <string>		// For file name
#include <iomanip>		// For setw
#include <fstream>		// For file input
using namespace std;


void IsEven(ifstream din);

int main ()
{  
	string fileName;

	cout << "Please enter a file name "<<endl;
	cin >> fileName;

	ifstream data;

	data.open(fileName.c_str());

	if(!data)
	{
		cout << "No such file found!" << endl;
	}
	else
	{
		IsEven(data);
	}
    return 0;
}

void IsEven(ifstream din)
{
	/*
		Purpose: Takes input from a file and determines how many even numbers and odd numbers are in the file

		Pre:	 File

		Post:	 How many even numbers are in the file and how many odd numbers are in the file
	*/
	int num;
	int even = 0;
	int odd = 0;
	din >> num;
	while(din)
	{
		if(num % 2 == 0)
			even = even ++;
		else
			odd = odd ++;
		din >> num;
	}

	cout << "There are " << even << " numbers in the file and " << odd << " numbers in the file." << endl;

}


here is the error message

1>c:\program files\microsoft visual studio 8\vc\include\fstream(675) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
 
void IsEven(ifstream& din)


As I remember copy contructor of fstream is private, please try change code to my variant
Pass the ifstream object as reference instead of value, i.e. modify the function prototype and implementation as follows:

void IsEven(ifstream& din);

Also, even = even ++; assigns the existing value of the variable to itself, and then post-increments the rvalue, which results in the value remaining 0. There's an easy fix for this: just replace the assignment with ++even;. The same applies for odd, obviously.
Topic archived. No new replies allowed.