DOCUMENT:Q130276 23-JUL-2001 [visualc] TITLE :FIX: C2065 Error on Functions Returns Template Class By Value PRODUCT :Microsoft C Compiler PROD/VER:winnt: OPER/SYS: KEYWORDS:kbCompiler kbCPPonly kbVCkbbuglist kbfixlist ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - The C/C++ Compiler (CL.EXE), included with: - *EDITOR Please do not choose this product*Microsoft Visual C++ 32-bit Edition* use 241, 265, 225, versions 2.0, 2.1 ------------------------------------------------------------------------------- SYMPTOMS ======== Any reference to a template function that returns a template class by value generates these errors: error C2065: 'function name' : undeclared identifier error C2064: term does not evaluate to a function C2065 RESOLUTION ========== Modify the function to return a reference to the template class instead of returning by value. In the "Sample Code" section of this article, function 'f1' returns a template class by value and fails. Function 'f2' returns a template class by reference and succeeds. STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was fixed in Microsoft Visual C++, 32-bit Edition, version 4.0. MORE INFORMATION ================ Sample Code ----------- /* Compile options required: none */ template class M { public: T m; }; template M f1(const M& x) {return x;} template M& f2(const M& x) {return x;} void test(void) { M x; M y = f1(x); // errors C2065 & C2064 M z = f2(x); // no error } NOTE: Although this has been fixed under Visual C++ 4.0, the definition of function f2: template M& f2(const M& x) {return x;} causes the following compilation errors: error C2446: [*] 'return' : no conversion from 'const class M' to 'class M &' (new behavior; please see help) error C2561: 'f2' : function must return a value To avoid this, the return value of that statement should be casted appropriately, as follows: M& f2(const M& x) {return (M&)x;} Additional query words: 2.00 2.10 9.00 9.10 ====================================================================== Keywords : kbCompiler kbCPPonly kbVC kbbuglist kbfixlist Technology : kbVCsearch kbAudDeveloper kbCVCComp Version : winnt: 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 2001.