Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting an error concerning enum (I think)

Tags:

c++

enums

I'm testing code remotely on a Solaris machine through SSH Secure Shell using c++. Not sure of what version anything is; Solaris, the c++/compiler, etc. (and don't know how to find out through SSH Secure Shell)...

This code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <errno.h>

using std::cout;
using std::cin;
using std::string;


enum STR_TO_INT_STATUS { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };

STR_TO_INT_STATUS str_to_int( int&, char const*, int );


int main()
  {
   int num;
   string str;
   STR_TO_INT_STATUS code;

   cout << "\nEnter a string containing numbers: ";

   cin >> str;

   code = str_to_int( num, str.c_str(), 0 );

   if( code == OVERFLOW || code == UNDERFLOW )
     cout << "\nThe number was out of int's range\n\n";
   else if( code == INCONVERTIBLE )
          cout << "\nThe string contained non-number characters\n\n";
        else if( code == SUCCESS )
               cout << "\nThe int version of the string is: " << num << "\n\n";
  }


STR_TO_INT_STATUS str_to_int( int &i, char const* s, int base )
  {
   char *end;
   long l;
   errno = 0;

   l = strtol( s, &end, base );

       if( ( errno == ERANGE && l == LONG_MAX ) || l > INT_MAX )
         return OVERFLOW;

       if( ( errno == ERANGE && l == LONG_MIN ) || l < INT_MIN )
         return UNDERFLOW;

   if( *s == '\0' || *end != '\0' )
     return INCONVERTIBLE;

   i = l;

   return SUCCESS;
  }

compiles and works fine... as you can see, it converts a string entered by the user into an int...

But when modified like so:

#include <iostream>
#include <string>
#include <cstdlib>
#include <errno.h>
#include <limits>
#include <cmath>

using std::cout;
using std::cin;
using std::string;
using std::numeric_limits;


int size_of( int ); //------------------------------------------------- :62

enum STR_TO_INT_STATUS { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };

STR_TO_INT_STATUS str_to_int( int&, char const*, int ); //------------- :84


int main()
  {
   int num;
   string str;
   string dummy;
   STR_TO_INT_STATUS code;

   system( "clear" );

   cout << "\nint's max limit: "
        << numeric_limits<int>::max()
        << "\nint's min limit: "
        << numeric_limits<int>::min()
        << "\n\nnumber of digits in the largest int: "
        << size_of( numeric_limits<int>::max() )
        << "\nnumber of digits in the smallest int: "
        << size_of( numeric_limits<int>::min() );

   cout << "\nEnter a string containing numbers: ";

   cin >> str;

   code = str_to_int( num, str.c_str(), 0 );

   if( code == OVERFLOW || code == UNDERFLOW )
     cout << "\nThe number was out of int's range\n\n";
   else if( code == INCONVERTIBLE )
          cout << "\nThe string contained non-number characters\n\n";
        else if( code == SUCCESS )
               cout << "\nThe int version of the string is: " << num << "\n\n";

   cout << "Press enter key to continue...";

   getline( cin, dummy );

   system( "clear" );

   return( 0 );
  }


int size_of( int num )
  {
   int length = 0;

   num = ( int )fabs( num );

   if( num == 0 )
     length = 1;
   else
     while( num > 0 )
       {
        length++;

        num /= 10;
       }

   return( length );
  }

STR_TO_INT_STATUS str_to_int( int &i, char const* s, int base )
  {
   char *end;
   long l;
   errno = 0;

   l = strtol( s, &end, base );

   if( ( errno == ERANGE && l == LONG_MAX ) || l > INT_MAX )
     return OVERFLOW;

   if( ( errno == ERANGE && l == LONG_MIN ) || l < INT_MIN )
     return UNDERFLOW;

   if( *s == '\0' || *end != '\0' )
     return INCONVERTIBLE;

   i = l;

   return SUCCESS;
  }

I get the following compile errors:

int_limits.cpp:16: error: expected identifier before numeric constant
int_limits.cpp:16: error: expected `}' before numeric constant
int_limits.cpp:16: error: expected unqualified-id before numeric constant
int_limits.cpp:16: error: expected `,' or `;' before numeric constant
int_limits.cpp:16: error: expected declaration before '}' token

I've looked for spelling errors, tried moving the location of the enum line around... I dunno what in the world is going on.

Any help with this issue would be GREATLY appreciated!

like image 899
Aaron Chauvin Avatar asked Jun 09 '11 07:06

Aaron Chauvin


People also ask

Is enum an int or long?

An enum type is represented by an underlying integer type. The size of the integer type and whether it is signed is based on the range of values of the enumerated constants.

Are enums signed or unsigned C++?

In short: you cannot rely on an enum being either signed or unsigned. Michael Burr's answer (which quotes the standard) actually implies you can rely on it being signed if you define an enum value as negative due to the type being able to "represent all the enumerator values defined in the enumeration".

Can we have enum inside enum in C?

It is not possible to have enum of enums, but you could represent your data by having the type and cause separately either as part of a struct or allocating certain bits for each of the fields.

Are all enums ints?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type. Specify the type after enum name as : type . The following defines the byte enum.


1 Answers

The include of cmath is defining preprocessor constants OVERFLOW as 3 and UNDERFLOW as 4. So the line declaring the enum becomes (if there are no other constants):

enum STR_TO_INT_STATUS { SUCCESS, 3, 4, INCONVERTIBLE };

which, of course is not valid syntax.

like image 147
vhallac Avatar answered Oct 01 '22 22:10

vhallac