Microsoft KB Archive/184089

= FIX: C2555 with Virtual Function Returning Template Object =

Article ID: 184089

Article Last Modified on 12/8/2003

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q184089



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.



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; }

