Article ID: 149327 - Last Review: July 5, 2005 - Revision: 1.1

FIX: C2440 Expanding Template with a Pointer to a Nested Class

This article was previously published under Q149327

On This Page

Expand all | Collapse all

SYMPTOMS

At the point where a class template is expanded using a pointer to a nested class, the compiler reports the following errors (generated by the sample code listed in this article):
[FileName](14) : error C2440: 'initializing' : cannot convert from 'class A::B' to 'class A::B *' (new behavior; please see help)
[FileName](14) : error C2439: 't' : member could not be initialized
Lines 13 and 14 in the sample code are:
template <class T> C<T>::C() : t(T())  // error
   {
				

RESOLUTION

There are three possible workarounds to this problem. Each is stated here in terms of the sample code included in this article:

  • Remove t(T()) from the initializer list and add t = T(); to the body of the constructor. This workaround assumes there is an operator= defined for class T.
  • Change C<A::B *> c; to C<A::B&> c; or C<A::B> c:
  • Change the definitions of class A and class B to that B is no longer nested in the definition of A.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

In this case, the problem is caused by the attempt to initialize the data member t.

Sample Code

/* Compile options needed: None */ 
template <class T> class C
{
public:
  C();
private:
  T t;
};

template <class T> C<T>::C() : t(T())  // error
{
}

class A
{
public:

   class B
   {
   };

};

main()
{

   C<A::B *> c;
   return 0;

}
				

APPLIES TO
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Enterprise Edition
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
Keywords: 
kbbug kbfix kbvc600fix KB149327
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations