Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

c++ - I'm getting an error concerning enum (I think)

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 << "
Enter a string containing numbers: ";

   cin >> str;

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

   if( code == OVERFLOW || code == UNDERFLOW )
     cout << "
The number was out of int's range

";
   else if( code == INCONVERTIBLE )
          cout << "
The string contained non-number characters

";
        else if( code == SUCCESS )
               cout << "
The int version of the string is: " << num << "

";
  }


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 == '' || *end != '' )
     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 << "
int's max limit: "
        << numeric_limits<int>::max()
        << "
int's min limit: "
        << numeric_limits<int>::min()
        << "

number of digits in the largest int: "
        << size_of( numeric_limits<int>::max() )
        << "
number of digits in the smallest int: "
        << size_of( numeric_limits<int>::min() );

   cout << "
Enter a string containing numbers: ";

   cin >> str;

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

   if( code == OVERFLOW || code == UNDERFLOW )
     cout << "
The number was out of int's range

";
   else if( code == INCONVERTIBLE )
          cout << "
The string contained non-number characters

";
        else if( code == SUCCESS )
               cout << "
The int version of the string is: " << num << "

";

   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 == '' || *end != '' )
     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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...