Topic: friend declaration to template classes


Author: "Fedor G. Pikus" <fedor_pikus@mentorg.com>
Date: Mon, 24 Sep 2001 17:27:59 GMT
Raw View
GCC gives a fairly meaningful error message if you uncomment (XX):
using typedef-name `AB' after `class'

Indeed,
friend AB;
works just fine. "A<B>" is already "class", no need to say "class AB".



Enno Kehrer wrote:
>
> Hi,
>
> look at code below and tell me why I cannot make a friend declaration to a
> typedefed template class.
> Perhaps this is this a specific vcc issue. Can anyone help me with that ?
>
> class B;
> template <class T>
> class A
> {
>     public:
>         void foo ( B & b )
>         {// Access private data in B
>             std::cout << b.data << std::endl;
>         }
> };
>
> typedef A<B> AB;    // Type substitution:  AB == A<B>
>
> class B
> {
> //  friend class AB;        // (XX) error C2248: 'data' : cannot access
> private member declared in class 'B'
>     friend class A<B>;      // ok
>     public:
>         B () : data(123) {}
>     private:
>         int data;           // Private data
> };
>
> int main(int argc, char* argv[])
> {
>     B b;                    // instance of B
>     AB ab;                  // instance of AB  (  A<B>  )
>                             // -> Activate XX above and you'll get:
>                             // error C2079: 'ab' uses undefined class 'AB'
>     ab.foo( b );            // A access B
>     return 0;
> }
>
> ///// --- End of Code --- /////
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]

--
                                  Fedor G. Pikus
http://www.speakeasy.org/~pikus/

Mentor Graphics Corporation         | Phone: (503) 685-4857
8405 SW Boeckman Road               | FAX:   (503) 685-1239
Wilsonville, Oregon 97070           |

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Enno Kehrer" <enno.kehrer@autodesk.com>
Date: Fri, 21 Sep 2001 16:52:39 GMT
Raw View
Hi,

look at code below and tell me why I cannot make a friend declaration to a
typedefed template class.
Perhaps this is this a specific vcc issue. Can anyone help me with that ?


class B;
template <class T>
class A
{
    public:
        void foo ( B & b )
        {// Access private data in B
            std::cout << b.data << std::endl;
        }
};

typedef A<B> AB;    // Type substitution:  AB == A<B>

class B
{
//  friend class AB;        // (XX) error C2248: 'data' : cannot access
private member declared in class 'B'
    friend class A<B>;      // ok
    public:
        B () : data(123) {}
    private:
        int data;           // Private data
};


int main(int argc, char* argv[])
{
    B b;                    // instance of B
    AB ab;                  // instance of AB  (  A<B>  )
                            // -> Activate XX above and you'll get:
                            // error C2079: 'ab' uses undefined class 'AB'
    ab.foo( b );            // A access B
    return 0;
}

///// --- End of Code --- /////



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]