Microsoft KB Archive/85478

= INFO: Error C2593 When Archiving an Enumerated Type =

Article ID: 85478

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft C/C++ Professional Development System 7.0
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.51
 * Microsoft Visual C++ 1.52 Professional Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 4.0 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 5.0 Learning Edition

-



This article was previously published under Q85478





SUMMARY
In ANSI C, enumerated types are synonyms for &quot;int&quot;; 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 &quot;archive << e;&quot; 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 

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( &quot;testfile&quot;, 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( &quot;testfile&quot;, CFile::modeRead, NULL ); CArchive archiveIn( &myFile, CArchive::load );

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

archiveIn >> (WORD&) today;

archiveIn.Close; }

Additional query words: 8.00 8.00c 9.00 9.10

Keywords: kberrmsg kbinfo kbcpponly kbcode kbcompiler KB85478

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.