"Problem with friend function"

This is a discussion on "Problem with friend function" within the C++ Programming forums, part of the General Programming Boards category; Hi All, I am facing compilition error for following code: Code: Angle.h: ------- #include <iostream> using namespace std; class Angle ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    "Problem with friend function"

    Hi All,

    I am facing compilition error for following code:

    Code:
    Angle.h:
    -------
    #include <iostream>
    using namespace std;
    
    class Angle
    {
          int deg;
          int min;
          double sec;             
    public:
           friend istream & operator >> (istream &, Angle &);
           friend ostream & operator << (ostream &, Angle &);
    
    };
    
    Angle.cpp:
    #include "iostream"
    #include "Angle.h"
    using namespace std;
    istream & operator>> (istream &in, Angle &A)
    {
        	in>>A.deg;
            in>>A.min;
            in>>A.sec;	
    }
    
    ostream & operator<< (ostream &out, Angle &A)
    {
           
              out<<"Deg: "<<A.deg<<endl;
    	      out<<"Min: "<<A.min<<endl;
    		  out<<"Sec: "<<A.sec<<endl;
    }
    
    int main()
    {
    	Angle A;
    	cin>>A;
    	cout<<A;
        return 0;
    
    }
    I am getting following errors, even though i am accessing private variables via friend function:

    c:\dev-cpp\examples\angle.cpp(82) : error C2248: 'deg' : cannot access private member declared in class 'Angle'
    c:\dev-cpp\examples\angle.h(10) : see declaration of 'deg'
    c:\dev-cpp\examples\angle.cpp(83) : error C2248: 'min' : cannot access private member declared in class 'Angle'
    c:\dev-cpp\examples\angle.h(11) : see declaration of 'min'
    c:\dev-cpp\examples\angle.cpp(84) : error C2248: 'sec' : cannot access private member declared in class 'Angle'
    c:\dev-cpp\examples\angle.h(12) : see declaration of 'sec'
    c:\dev-cpp\examples\angle.cpp(90) : error C2248: 'deg' : cannot access private member declared in class 'Angle'
    c:\dev-cpp\examples\angle.h(10) : see declaration of 'deg'
    c:\dev-cpp\examples\angle.cpp(91) : error C2248: 'min' : cannot access private member declared in class 'Angle'
    c:\dev-cpp\examples\angle.h(11) : see declaration of 'min'
    c:\dev-cpp\examples\angle.cpp(92) : error C2248: 'sec' : cannot access private member declared in class 'Angle'
    c:\dev-cpp\examples\angle.h(12) : see declaration of 'sec'

    Regards,
    Siddu

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Avoid putting using namespace statments in your headers. They are fine in code files but not in headers. Explicitly qualify any necessary objects with std:: where necessary. Also, implement header guards. Your header file should look more like:
    Code:
    #ifndef ANGLE_H
    #define ANGLE_H
    
    #include <iostream>
     
    class Angle
    {
          int deg;
          int min;
          double sec;            
    public:
           friend std::istream & operator >> (std::istream &, Angle &);
           friend std::ostream & operator << (std::ostream &, const Angle &);
     
    };
    
    #endif

    Your overloaded operator<</operator>> functions need to return the stream object that has been passed in:
    Code:
    istream& operator>> (istream &in, Angle &A)
    {
        in>>A.deg;
        in>>A.min;
        in>>A.sec;
        return in;
    }
     
    ostream & operator<< (ostream &out, const Angle &A)
    {
        out<<"Deg: "<<A.deg<<endl;
        out<<"Min: "<<A.min<<endl;
        out<<"Sec: "<<A.sec<<endl;
        return out;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,394
    Quote Originally Posted by Shidlingayya View Post
    Code:
    Angle.h:
    -------
    #include <iostream>
    using namespace std;
    you really should not have using declarations in header files. it pollutes the global namespace in places where you might not want it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with the "friend template" snippet
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2008, 01:14 PM
  2. A simple question about "friend" in template classes
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2008, 02:35 AM
  3. the "friend" function .... ?
    By twomers in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2005, 04:22 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

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