INFO: Error C2593 When Archiving an Enumerated Type

Last reviewed: September 4, 1997
Article ID: Q85478

The information in this article applies to:
  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0

SUMMARY

In ANSI C, enumerated types are synonyms for "int"; however, in C++, enumerated types are distinct integral types but are not integers themselves. Therefore, if e is an enumerated type and archive is of type CArchive, compiling these statements

   archive << e;
   archive >> e;

results in the following error messages from Microsoft C/C++ versions 7.0 and 8.0:

   error C2593: 'operator <<' is ambiguous
   error C2593: 'operator >>' is ambiguous

The compiler cannot choose which operator to use, because the enumerated type could be promoted to match any integral type. The programmer must specify which integral type the operator should use.

MORE INFORMATION

The following are the prototypes taken from AFX.H. These are the functions that the compiler chooses from when it compiles the statement "archive << e;"

   CArchive& operator<<( BYTE by );
   CArchive& operator<<( WORD w );
   CArchive& operator<<( LONG l );
   CArchive& operator<<( DWORD dw );

where BYTE, WORD, LONG, and DWORD are defined in AFX.H as follows:

   typedef unsigned char    BYTE;   // 8-bit unsigned entity
   typedef unsigned short   WORD;   // 16-bit unsigned number
   typedef long             LONG;   // 32-bit signed number
   typedef unsigned long    DWORD;  // 32-bit unsigned number

Therefore, the following statements compile without error:

   archive << (WORD) e;
   archive >> (WORD&) e;

   -or-

   archive << (unsigned short) e;
   archive >> (unsigned short&) e;

NOTE: The following statements do not compile because they are also ambiguous:

   archive << (int) e;
   archive >> (int&) e;

In these cases, the compiler cannot choose which operator to use, because the int could be promoted to match any integral type. Again, the programmer must specify which integral type the compiler should use.

The following is a complete example demonstrating how to store and load an enumerated type:

Sample Code

   /* Compile options needed: none
   */

   #define _DOS
   #include <afx.h>

   enum DAY
   {
      sunday, monday, tuesday, wednesday, thursday, friday, saturday
   };

   void main( void );

   void main( )
   {
      CFile myFile;

   // Open the file for writing and associate an archive object with it.

      myFile.Open( "testfile", CFile::modeWrite, NULL );
      CArchive archiveOut( &myFile, CArchive::store );

      enum DAY today = tuesday;

   // Use the insertion operator to store the enumerated value
   //    archiveOut << today;
   // will generate an error

      archiveOut << (WORD) today;

      archiveOut.Close();
      myFile.Close();

   // Open the file for reading and associate an archive object with it.

      myFile.Open( "testfile", CFile::modeRead, NULL );
      CArchive archiveIn( &myFile, CArchive::load );

   // Use the extraction operator to load the enumerated value
   // "archiveIn >> today;" would generate an error

      archiveIn >> (WORD&) today;

      archiveIn.Close();
   }


Additional query words: 8.00 8.00c 9.00 9.10
Keywords : CPPIss kbcode
Version : MS-DOS:7.0; WINDOWS:1.0,1.5,1.51,1.52; WINDOWS NT:1.0,2.0,2.1,4.0,5.0
Platform : MS-DOS NT WINDOWS
Issue type : kberrmsg kbinfo


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.

Last reviewed: September 4, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.