Having trouble with void functions.

Doing homework for a class, and I'm having trouble getting my program to stop throwing errors back at me. I got through my pants-on-head moment which prompted me to make this account, but now the program is spitting back this error:
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\fstream(1116): 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 (x86)\Microsoft Visual Studio 10.0\VC\include\ios(176) : 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_ofstream<_Elem,_Traits>::basic_ofstream(const std::basic_ofstream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\fstream(890): 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 (x86)\Microsoft Visual Studio 10.0\VC\include\ios(176) : 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> ]

This is my program, as it stands:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

void openFiles(ifstream&, ofstream&);
void initialize();
void sumGrades(ifstream, ofstream, int&, int&, double&, double&);
void averageGrade(int, int, double, double, double&, double&);
void printResults(ifstream, ofstream, int, int, double, double, double, double);

int main()
{
	cout << "See final.txt for results." << endl;

	initialize();

	system("pause");

	return 0;
}

void initialize()
{
	int countFemale = 0;
	int countMale = 0;
	double sumFemaleGPA = 0.0;
	double sumMaleGPA = 0.0;
	double averageMaleGPA = 0.0;
	double averageFemaleGPA = 0.0;
	ifstream inGrades;
	ofstream outGrades;
	openFiles(inGrades, outGrades);
	sumGrades(inGrades, outGrades, countFemale, countMale, sumFemaleGPA, sumMaleGPA);
	averageGrade(countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageMaleGPA, averageFemaleGPA);
	printResults(inGrades, outGrades, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageMaleGPA, averageFemaleGPA);
}
void openFiles(ifstream& inGrades, ofstream& outGrades)
{
	inGrades.open("Ch7_Ex6Data.txt");
	outGrades.open("final.txt");

	outGrades << fixed << showpoint << setprecision(3);
}
void sumGrades(ifstream inGrades, ofstream outGrades, int& countFemale,
		int& countMale, double& sumFemaleGPA, double& sumMaleGPA)
{
	char gender;
	double grade;

	while (inGrades)
	{
		inGrades >> gender >> grade;
		if (gender == 'm')
		{
			countMale++;
			sumMaleGPA = sumMaleGPA + grade;
		}
		else
		{
			countFemale++;
			sumFemaleGPA = sumFemaleGPA + grade;
		}
	}
}
void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,
		double& averageMaleGPA, double& averageFemaleGPA)
{
	averageMaleGPA = sumMaleGPA / countMale;
	averageFemaleGPA = sumFemaleGPA / countFemale;
}
void printResults(ifstream inGrades, ofstream outGrades, int countFemale, int countMale,
		double sumFemaleGPA, double sumMaleGPA, double averageMaleGPA, double averageFemaleGPA)
{
	outGrades << "Processing Grades." << endl;
	outGrades << inGrades;
	outGrades << "Sum Female GPA:" << sumFemaleGPA << endl;
	outGrades << "Sum Male GPA:" << sumMaleGPA << endl;
	outGrades << "Male count:" << countMale << endl;
	outGrades << "Female count:" << countFemale << endl;
	outGrades << "Male Average:" << averageMaleGPA << endl;
	outGrades << "Female Average:" << averageFemaleGPA << endl;
	inGrades.close();
	outGrades.close();
}
ifstreams and ofstreams don't have copy constructors. You're expected to pass them by reference to your functions.

EDIT: Eh, I really should give a hint for this one. Check lines 9, 11, 46, and 73.

Good luck!

-Albatross
Last edited on
I'm not exactly savvy with the parlance. Can you put that into layman's terms?

Or possibly give an example of what you mean?

EDIT: After seeing what those lines had in common and removing the (supposedly) offending content, I now have a new error message.

-snipped for pointlessness-
I can only assume this has to be fixed with the passing by reference. Which I don't really get.

EDIT2: Ah, I see where I went wrong. Got some help from another forum. I need to include the &. Thank you for your help!
Last edited on
Alright.
1
2
3
4
5
6
7
8
#include <fstream>

int main() {
    std::fstream   a;
    std::fstream   b = a; //ERROR!
    std::fstream&  c = a; //Works!
    return 0;
}


Why does b fail? Because the function that gets fstreams ready for use that takes an fstream (reference) as an argument has been disabled. In short, fstreams have been made... difficult to copy, and for a good reason.

Why does c work? Because creating a reference from an fstream is perfectly valid, as you never call that disabled function in the process. As long as the fstream isn't copied, there shouldn't be any problems there. :)

EDIT: Passing by reference avoids copying anything you send to a function, and rather just gives you something that if you modify, will also affect the original function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

void pb_value(int arg) {
    ++arg;
}
void pb_refer(int& arg) {
    ++arg;
}

int main() {
    int a = 5;
    std::cout << "Initial value of a: " << a << ".\n";
    pb_value(a); //Copies a and affects that copy.
    std::cout << "Value of a after pass by value: " << a << ".\n";
    pb_refer(a); //Creates a reference and changes a through it.
    std::cout << "Value of a after pass by reference: " << a << ".\n";
    return 0;
}
Initial value of a: 5.
Value of a after pass by value: 5.
Value of a after pass by reference: 6.


-Albatross
Last edited on
Topic archived. No new replies allowed.