What is wrong with this code?

I am reading Accelerated C++ and I do not understand why this code:

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
#include<algorithm>
#include <iomanip>
#include<ios>
#include<iostream>
#include<stdexcept>
#include<string>
#include<vector>
#include"grade.h"
#include"student_info.h"
#include<conio.h>
using std::cin;               using std::setprecision;
using std::cout;              using std::sort;
using std::domain_error;      using std::streamsize;
using std::endl;              using std::string;
using std::max;               using std::vector;

int main (){



	vector<student_info> students;
	student_info record;
	string::size_type maxlen = 0; // length of the longest name

	// read and store all of the students data.
	// invariant: students contains all of the student records read so far
	//            maxlen contains the length of the longest name in students
	while (read (cin,record)){
		// find the longest name
		maxlen = max (maxlen, record.name.size ());
		students.push_back (record);
	}

	// alphabetize the student records
	sort (students.begin (), students.end () ,compare);

	// write the names and grades
	for (vector<student_info>::size_type i = 0;i != students.size (); ++i){
// write the name padded on the right to maxlen + 1 characters
		cout<<students [i].name
			<< string (maxlen = 1 - students [i].name.size(), ' ');


		// compute and write the grade
		try {
			double final_grade = grade (students [i]);
			streamsize prec = cout.precision ();;
			cout<< setprecision (3) << final_grade
				<< setprecision (prec);
} catch (domain_error e){
	cout<<e.what ();
		}
		cout<<endl;
	}


_getch (); // note I added this in myself
return 0;


}


with the header files:
1
2
3
4
5
6
7
8
9
10
11
#ifndef GUARD_grade_h
#define GUARD_grade_h

// grade.h

#include<vector>
#include"student_info.h"
double grade (double,double,double) ;
double grade (double , double, const std::vector<double>&) ;
double grade (const student_info) ;
#endif 


1
2
3
4
5
6
7
8
9
#ifndef GUARD_median_h
#define GUARD_median_h
// median.h - the final version
#include<vector>

double median (std::vector<double>);


#endif 


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef GUARD_student_info
#define GUARD_student_info
// student_info.h header file

#include<iostream>
#include<vector>
#include<string>

struct student_info {


	std::string name;
	double midterm, final;
	std::vector<double> homework;

};

bool compare (const student_info&, const student_info&);
std::istream& read (std::istream, student_info&);
std::istream& read_hw (std::istream&, std::vector<double>&);

#endif 


is yielding this in the output dialog:

1>------ Build started: Project: Revised Grading Program, Configuration: Debug Win32 ------
1> source code.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\istream(860): 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 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_istream<_Elem,_Traits>::basic_istream(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

And also I do not really much understand the error itself. Thanks
Last edited on
I think this is because read() has an istream passed by value. It should be by reference. What the error says is that there is something wrong with basic_istream's copy constructor.
I can't explain why the error is so unclear and why copying a stream is not allowed though.
Thanks hamsterman, I am not sure how to fix it, does any body have any clue?
Last edited on
Streams have never been copyable. The error exists because the copy constructor is private.

Your read() function should have the type:

std::istream& read (std::istream&, student_info&);

Hope this helps.
Should help, I will modify the code tomorrow. Thank You.
I changed std::istream& read (std::istream, student_info&); to std::istream& read (std::istream&, student_info&); And it yielded yet another error, It now has a problem with my bool compare and my double grade and yet again an istream error.




I apologise for having to ask again but what exactly should I do, should I move on or should I very hard to fix it (likely to no aveil).
Last edited on
Topic archived. No new replies allowed.