DOCUMENT:Q184089 11-FEB-2002 [visualc] TITLE :FIX: C2555 with Virtual Function Returning Template Object PRODUCT :Microsoft C Compiler PROD/VER::4.0,4.0a,4.1,4.2,5.0,6.0 OPER/SYS: KEYWORDS:kbCompiler kbCPPonly kbLangCPP kbVC kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600 ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual C++, versions 4.0, 4.0a, 4.1 - Microsoft Visual C++, 32-bit Enterprise Edition, versions 4.2, 5.0, 6.0 - Microsoft Visual C++, 32-bit Professional Edition, versions 4.2, 5.0, 6.0 - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 ------------------------------------------------------------------------------- SYMPTOMS ======== A virtual function returning a templated class object and a derived class defining the virtual function, causes the following compiler error: error C2555: 'MyDerived::TestFunc' : overriding virtual function differs from 'MyBase::TestFunc' only by return type or calling convention RESOLUTION ========== Define the base class function to be nonvirtual. 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++ .NET. MORE INFORMATION ================ Sample Code ----------- template class TestClass { public: T val; TestClass(T num){val=num;} }; class MyBase { public: int value; virtual TestClass TestFunc(void) ; /* Make this function nonvirtual to fix C2555 error. */ /* A pure virtual function also has the same problem. */ }; template class MyDerived : public MyBase { public: virtual TestClass TestFunc(void) { return TestClass(3.14f); } }; MyDerived x; void main(void) { x.TestFunc(); } REFERENCES ========== (c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Mark Hagen, Microsoft Corporation. Additional query words: CL C2555 CPP CXX ====================================================================== Keywords : kbCompiler kbCPPonly kbLangCPP kbVC kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600bug kbNoUpdate Technology : kbVCsearch kbVC400 kbAudDeveloper kbVC410 kbVC420 kbVC500 kbVC600 kbVC32bitSearch kbVC400a kbVC500Search Version : :4.0,4.0a,4.1,4.2,5.0,6.0 Issue type : kbbug Solution Type : kbfix ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 2002.