FIX: Template Friend Function Causes Compiler Error C2248

ID: Q166109


The information in this article applies to:
  • The C/C++ Compiler (CL.EXE), used with:
    • Microsoft Visual C++, 32-bit Editions, version 5.0


SYMPTOMS

The compiler generates a compiler error C2248 as follows:

Error C2248: 'member' : cannot access member declared in class 'class'
under the following circumstances:

  • If a class declares a template function as a friend function, and


  • If the template function definition appears after the class definition.



RESOLUTION

Do not declare a template function as a friend of a class.


STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.This problem was corrected in Microsoft Visual C++ version 6.0.


MORE INFORMATION

The following sample demonstrates the problem and the workaround.

Sample Code


   /*
   Compile option: None
   */ 

   class X;

   template <class T> void AFunction(X &x, T &t);

   class X
   {
   private:

      int m_n;

   public:

      template <class T> friend void AFunction(X &x, T &t) ;

   };

   template <class T> void AFunction(X &x, T &t)
   {

      x.m_n = t; // C2248 here.

   }

   int main()
   {
      X x;
      int n;
      AFunction(x, n);

           return 0 ;
   }
 
NOTE: Defining the template function before the class definition eliminates the compiler error C2248. But the compiler does not generate any code when you call the template function.

Additional query words:

Keywords : kbtool kbVC500bug kbVC600fix
Version : winnt:5.0
Platform : winnt
Issue type : kbbug


Last Reviewed: March 27, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.