friend operator >> and private variables

I'm unable to access private variables belonging to the object class Date, which my overloaded >> operator is a friend of. I can't see anything in my code that would be causing this error. Please give me a nod in the right direction. Disclaimer: this code has been written as a part of a homework assignment for my college course. I am not asking to be given corrected code, just an explanation of what I am doing wrong and a hint as to how to correct it. Thanks in advance, the .h file and the definition of the problematic overloaded operator from the implementation file are below:

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
#ifndef DATE_H
#define DATE_H

#include <string>
using namespace std;

class Date {
   public:
      // Initializes a date to the default value of January 1, 1970.
      Date();

      // Initializes a date to the values in the parameters.  If the
      // date is not legal, sets the date to one of the legal values.
      Date(int m, int d, int y);

      // Returns the month stored by the class
      string getMonth() const;

      // Returns the month stored by the class as a number
      int getMonthNum() const;

      // Returns the day stored by the class
      int getDay() const;

      // Returns the year stored by the class
      int getYear() const;

      // solicit the date from the user
      void input();

      // output the date in a nice format
      void output() const;
	  
	  // overloaded << operator
	  friend ostream& operator <<(ostream& os, const Date& d);
	  
	  // overloaded >> operator
	  friend istream& operator >>(istream& is, Date& d);

   private:
      int month, day, year;
	  void adjust();
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
// solicit the date from the user
istream& operator >>(ostream& is, Date& d)
{
	cout << "Input month: ";
	is >> d.month;
	cout << "Input day: ";
	is >> d.day;
	cout << "Input year: ";
	is >> d.year;
	d.adjust();
	return is;
}


The error message states that the vars (month, day, year) are declared as private in the header file and then a reference is made to the lines where I attempt to access these in the .cpp file and it reads: "in this context".

Thanks again, folks!
Last edited on
Don't paraphrase the error messages.
istream& operator >>(ostream& is, Date& d) read carefully
bar.cpp:38:7: error: ‘int Date::month’ is private
bar.cpp:45:10: error: within this context
bar.cpp:45:5: error: no match for ‘operator>>’ in ‘is >> d.Date::month’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘int’)
bar.cpp:51:9: error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from expression of type ‘std::ostream {aka std::basic_ostream<char>}’



Also, you shouldn't using namespace in headers. All the sources that include it would get polluted
Last edited on
point taken, but that's how our instructor has us do it... anyway, i've read the error messages very carefully... I still don't see the problem as it's a friend function and should not have trouble accessing a private member variable. What am i missing?
As ne555 said:

read carefully

This is your declaration of the friend operator:

friend istream& operator >>(istream& is, Date& d);

This is the definition of the operator which is trying to access the private members:

istream& operator >>(ostream& is, Date& d)

See anything noteworthy about those two lines?
Last edited on
Ugh... I thought I already fixed that error, thanks guys.
You're welcome :)
Topic archived. No new replies allowed.