Thread: Question regarding classes

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    22

    Question regarding classes

    I have this class in a project for school:

    Code:
    class info
    {
    friend istream & operator >>(istream &, info&);
    friend ostream & operator <<(ostream &, const info &);
    public:
    	bool operator<( const info & ) const;
    	const info & operator=(const info & );
        void setId(int);
    	bool  operator==(const info &) const;
    
    private:
    	char name[20];
    	int id;
    	int age;
    };
    Now for the << and >> operators, I have this:

    Code:
     istream & operator >>(istream & in, info& l)
     {
    	 in >> l.age >> l.id >> l.name;
    	 return in;
     }
     ostream & operator <<(ostream & out, const info & l)
     {
    	 out << l.age <<' ' << l.id <<' ' << l.name;
    	 return out;
     }
    VC++ gives me a compile error for both of those saying "error C2248: 'age' : cannot access private member declared in class 'info'." This error occurs for each of the 3 variables. It is my understanding that a friend is able to access all private members of the class. So, why am I getting this error? Thanks.

    -Tristan

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I got similar errors a long time ago, I never understood why.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    So, why am I getting this error
    Because there is a bug in MSVC.

    Try downloading the latest service pack for MSVC. That should solve your problems.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I compiled with Dev without problems.
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Microsoft doesn't have any friends.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 01:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 07:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM