"Error C2248" complier error when you declare a template parameter type as a friend class of a template class in Visual C++
Article ID: 168384
Article Last Modified on 1/9/2006
APPLIES TO
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++ 6.0 Standard Edition
- Microsoft Visual C++ .NET 2003 Standard Edition
- Microsoft Visual C++ .NET 2002 Standard Edition
- Microsoft Visual C++ 2005 Express Edition
This article was previously published under Q168384
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
This behavior is by design as per C++ standard section
7.1.5.3 paragraph 2.
Keywords: kbtshoot kbcompiler kbcpponly kberrmsg kblangcpp kbprb KB168384