home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,572 of 33,346   
   =?windows-1252?Q?Daniel_Kr=FCgler?= to All   
   Re: member function as friend of a class   
   16 Oct 11 16:09:16   
   
   970ee117   
   From: daniel.kruegler@googlemail.com   
      
   Am 16.10.2011 15:29, schrieb Ethan:   
   >   I'm trying to have a member function of a class to be a friend of   
   > another class, however, i get this error   
   > Am i missing anything?   
      
   You are missing that for a friend declaration to another class's member   
   function the other class must be complete at the point of the friend   
   declaration.   
      
   > main.cpp:10: error: invalid use of incomplete type ‘struct B’   
   > main.cpp:3: error: forward declaration of ‘struct B’   
   >   
   > #include   
   >   
   > class B;   
   >   
   > class A {   
   > int a;   
   > public : A() { a = 5; }   
   >          void sample(B);   
   >          friend void B::sample();   
   > };   
   >   
   > class B {   
   > public : void sample() {   
   >              //printf("%d\n",obj.a);   
   >          }   
   > };   
      
   This requires a redesign to make this well-formed, e.g.   
      
   #include    
      
   class A;   
      
   class B {   
   public :   
      void sample();   
   };   
      
   class A {   
   int a;   
   public : A() { a = 5; }   
      void sample(B);   
      friend void B::sample();   
   };   
      
   void B::sample() {   
      //printf("%d\n",obj.a);   
   }   
      
   HTH & Greetings from Bremen,   
      
   - Daniel Krügler   
      
      
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca