BUG: C2248: No Access for Friend Class in a Template Class

Last reviewed: May 21, 1997
Article ID: Q168384
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Editions, version 5.0

SYMPTOMS

Declaring a template parameter type as a friend class of a template class, causes the compiler error C2248:

   error C2248: 'xx' : cannot access private member declared in class
   'XX<class T>'

Member access is not granted to friend class T, where T is a template parameter of the template class XX.

CAUSE

The compiler treats typename T as a regular C++ class and not a template parameter type. This implies the following:

  • typename T, where T is a template parameter type is not granted access to private members of the template class.
  • A regular C++ class named class T is granted access to private members of the template class.

RESOLUTION

Do not declare a template parameter type as a friend of a template class. Specify the friend class using the regular C++ class name.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The following sample code demonstrates the problem.

Sample Code

   /*
   * Compiler Options: none
   */

   template <typename T>
   class Test
   {
      //template parameter T declared as friend class
      friend class T ;
      int m_i ;
   } ;


   class B
   {
   public:
      //Cannot access private member of template class Test
      //since class B is not recognized as friend class of
      //template class Test
      B() { tB.m_i = 0 ;} //C2248 here
      Test<B> tB ;
   } ;

   int main()
   {
      B bObj ;
      return 0 ;
   }
 

	
	


Keywords : CPPIss CPPLngIss kberrmsg kbtool vcbuglist500 kbbuglist
Version : 5.0
Platform : NT WINDOWS
Issue type : kbbug


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.