FIX: C2084 When Template Class Constructor Has No Body (150573)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Editions 4.1
  • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
  • Microsoft Visual C++, 32-bit Professional Edition 4.2

This article was previously published under Q150573

SYMPTOMS

The following error occurs when a template class is explicitly instantiated and it has more than one constructor, at least one of which does not have a body defined and at least one of which is defined outside of the class:
error C2084: function '<function name>' already has a body

WORKAROUND

This is the preferred workaround. Give every constructor a body. The body is defined inside or outside the template class.

-or-

  • Place all the existing constructor bodies inside the template class definition.

    NOTE: You get the following warning associated with the explicit instantiation request:
    warning C4661: <symbol> : no suitable definition provided for explicit instantiation request
    This warning is issued when a template class does not have all its members defined and an explicit instantiation request is made. This warning also causes the compiler to ignore the instantiation request. -or-

  • Instantiate the template class implicitly rather than explicitly. In this case, the instantiation occurs when declaring an object using the template class, but the C2084 does not.
NOTE: the sample code in this article gives examples of explicit versus implicit template instantiation.

STATUS

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

MORE INFORMATION

Sample Code

   /* Compile options needed: /c
   */ 

   template <class T>
   class A {
   public:
       A();             // give this ctor a body for workaround 1
       A(const T&);
   };

   template <class T> A<T>::A(const T&) {}; // move this ctor body
                                            // inside the class
                                            // definition for
                                            // workaround 2

   template A<int>;     // this is an explicit instantiation -
                        // comment this line out and uncomment the
                        // next line for workaround 3
   // A<int> x = 3;     // this is an implicit instantiation
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbbug kbCompiler kbCPPonly kbfix kbVC500fix KB150573