Microsoft KB Archive/102701

{|
 * width="100%"|

PRB: C2609 or C4270 Error from IOMANIP.H Contents

 * }

Q102701

1.00 1.50 WINDOWS kbprg kbprb -- The information in this article applies to: - Microsoft Visual C++ for Windows versions 1.0 and 1.5 -- SYMPTOMS ======== An attempt to compile a C++ application that uses the functions declared in IOMANIP.H fails and Visual C++ generates messages such as the following. When the compiler command line specifies the /Za option switch: error C2609: 'initializing' : cannot implicitly convert a non-lvalue 'class ::__SMANIP_int ' function return to a 'class ::__SMANIP_int &' that is not const When the compiler command line does not contain the /Za option switch or when it contains the /Ze option switch: warning C4270: 'initializing' : do not initialize a non-const 'class ::__SMANIP_int __near &' with a non-lvalue 'class ::__SMANIP_int ' function return Microsoft C/C++ version 7.0 does not generate these messages. The Visual C++ online help files do not document the C4270 warning message. CAUSE ===== The C4270 is a new warning message introduced starting with Microsoft C/C++ compiler version 8.0 for MS-DOS, which is included with Visual C++ version 1.0 for Windows. Bjarne Stroustrup's book "Annotated C++ Reference Manual" contains an ambiguity regarding initializing const and non-const references. To address this ambiguity, the C4270 warning was added to the compiler but it was overlooked in the documentation. The IOMANIP.H header file contains examples of the assignment ambiguity, and the compiler correctly generates these errors or warnings when it processes the header file. RESOLUTION ========== The warning message is self-explanatory: the function return value is not an lvalue, therefore, you should not initialize a non-const reference with it. To work around this problem, perform one of the following four steps: - Modify the compiler command line to remove the /Za option switch. - Modify your source code to specify a pragma to turn off the warning message. With this method, you can turn the warning on and off in the code as desired, or you can disable the warning for the entire compilation. - Compile the code at warning level 1. - Modify the insertion operator declarations in IOMANIP.H to accept const references instead of non-const references. MORE INFORMATION ================ This error occurs in the sample code listed in Example 3 (page 369) in Chapter 18, "Fundamentals of iostream Programming," of the "Class Library User's Guide." The following sample code demonstrates this problem and how to use a pragma to control the warning: Sample Code --- /* * Compiler options needed: /c /W2 */ #include #include void main { //#pragma warning(disable: 4270) cout << setw(4) << "test" << endl; //#pragma warning(default: 4270) } Additional reference words: 1.00 1.50 8.00 8.00c SMANIP IMANIP OMANIP IOMANIP KBCategory: kbprg kbprb KBSubcategory: CPPLngIss

Keywords : kb16bitonly

Issue type :

Technology : kbVCsearch kbAudDeveloper kbvc150 kbvc100