PRB: C2609 or C4270 Error from IOMANIP.H Contents

ID: Q102701

1.00 1.50 WINDOWS kbprg kbprb

The information in this article applies to:

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:

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 <iostream.h>
#include <iomanip.h>

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

Last Reviewed: July 18, 1997