Microsoft KB Archive/148808

= FIX: C2670: Template Function Cannot Convert Parameter =

Article ID: 148808

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Professional Edition
 * Microsoft Visual C++ 5.0 Learning Edition

-



This article was previously published under Q148808



SYMPTOMS
The following error is generated:

error C2670: 'AFunction' : the template function cannot convert parameter 1 from type 'class Derived *'

in code containing all of the following:


 * A template class 'Derived' from another template class 'Base'.
 * 'AFunction' taking a pointer to the 'Base' as a parameter.
 * A call to the 'AFunction' passing a pointer to 'Derived' before instantiating any object of that type.

See the sample code in this article for an example.



RESOLUTION
To work around this problem, either explicitly instantiate 'Derived' or create a dummy object of type 'Derived' before calling the 'AFunction'.

See the Sample Code for an example.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Visual C++, 32-bit Edition, version 4.1.



Sample Code
/* Compile options needed: None

template  class Base { public: T data; int i; };

template  class Derived : public Base { public: int j; };

template  void AFunction(Base* b) { return; }

void main {  // Uncomment one of the following two statements to work around the

// problem. If the template class definition is in a header file, // you may want to include one of the following two statements at  // the end of that file.

// template Derived ; // Derived dummy;

Derived *j;

AFunction(j);  // Gives C2670 here }

Additional query words: 4.00

Keywords: kbbug kbfix kbcpponly kbcode kbcompiler KB148808

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.