Problem with Standard library (istream)

I have a problem with only this function, no idea why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
istream& Student_info::read(istream& is)
{
	delete cp;

	char ch;
	cout << "Type U for undergraduate, or G for graduate" << endl;
	is >> ch; // get record type

	if (ch == 'U')
		cp = new Core(is); // When I pass is, I get the error.
	else if (ch == 'G')
		cp = new Grad(is); // When I pass is, I get the error.
	else
	{
		cp = 0;
		cout << "You didn't pick G or U" << endl;
	}

	return is;
}


Here's the error

Error 1 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream 860
Oh noes!

Probably, the constructors for Core and Grad take an ifstream as an argument, not an ifstream&. The common ways of copying a stream object (=, copy constructors, etc) are protected, meaning you can't (easily) copy a stream object, and I don't think they were meant to be copied.

-Albatross
No, it's the same arguments. Here's Core and Class

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
#ifndef GUARD_Core_h

#define GUARD_Core_h

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

class Core
{
	// Friends
	friend double grade(const Core&);
	friend class Student_info;
public:
	// Constructors
	Core(): midterm(0), final(0) { }
	Core(std::istream is) { read(is); }
	// Virtual destructor
	virtual ~Core() {}
	// Virtual functions
	virtual std::istream& read(std::istream&);
	virtual double grade() const;
	/*Virtual function: If the argument is a Grad object,
	it will run the Grad::grade function; if the argument is a Core object, it will run the Core::grade
	function.*/
	/*This run-time selection of the virtual function to execute is relevant only when the function is called
	through a reference or a pointer.*/
	/*In contrast, a reference or pointer
	to a base-class object may refer or point to a base-class object, or to an object of a type derived
	from the base class, meaning that the type of the reference or pointer and the type of the object to
	which a reference or pointer is bound may differ at run time. It is in this case that the virtual
	mechanism makes a difference.*/
	std::string name() const;
	bool valid() const { return homework.empty(); }
protected:
	virtual Core* clone() const { return new Core(*this); }
	// Accessible to derived classes
	std::istream& read_common(std::istream&);
	double midterm, final;
	std::vector<double> homework;
private:
	// Accessible only to Core or friends
	std::string n;
};

bool compare(const Core&, const Core&);
bool compare_Core_ptrs(const Core*, const Core*);
std::istream& read_hw(std::istream&, std::vector<double>&);

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef GUARD_Grad_h

#define GUARD_Grad_h

#include "Core.h"

class Grad: public Core
{
public:
	Grad(): thesis(0) { }
	Grad(std::istream& is) { read(is); }
	// as defined in ยง13.1.2/230; Note: grade and read are virtual by inheritance
	double grade() const;
	std::istream& read(std::istream&);
protected:
	Grad* clone() const { return new Grad(*this); }
private:
	double thesis;
};

#endif 
Last edited on
Core.h: Line 17: Told ya.

-Albatross
Damn, I'm impressed. Thanks alot Albatross.
Topic archived. No new replies allowed.