简体   繁体   English

我收到有关枚举的错误(我认为)

[英]I'm getting an error concerning enum (I think)

I'm testing code remotely on a Solaris machine through SSH Secure Shell using c++.我正在使用 c++ 通过 SSH Secure Shell 在 Solaris 机器上远程测试代码。 Not sure of what version anything is;不确定是什么版本; Solaris, the c++/compiler, etc. (and don't know how to find out through SSH Secure Shell)... Solaris、c++/编译器等(不知道如何通过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...编译并且工作正常...如您所见,它将用户输入的字符串转换为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!任何有关此问题的帮助将不胜感激!

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): cmath的包含将预处理器常量OVERFLOW定义为 3,将UNDERFLOW定义为 4。因此声明枚举的行变为(如果没有其他常量):

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

which, of course is not valid syntax.这当然不是有效的语法。

I think it's the following line:我认为这是以下行:

int size_of( int );

...should be something more like: ...应该更像是:

int sizeOfInt = size_of( int );

edit编辑

I just compiled it on my machine and it's your OVERFLOW definition... #define's are evil!我刚刚在我的机器上编译它,这是你的OVERFLOW定义......#define 是邪恶的!

Try this:尝试这个:

enum STR_TO_INT_STATUS { STR_TO_INT_SUCCESS, STR_TO_INT_OVERFLOW, STR_TO_INT_UNDERFLOW, STR_TO_INT_INCONVERTIBLE};

My guess is that either <limits> or <cmath> have defines for one or more of SUCCESS , OVERFLOW , UNDERFLOW , INCONVERTIBLE , which causes your enum identifiers to be converted to "numeric constants", which in turn would cause the errors that you are seeing.我的猜测是<limits><cmath>定义了SUCCESSOVERFLOWUNDERFLOWINCONVERTIBLE中的一个或多个,这会导致您的枚举标识符转换为“数字常量”,这反过来会导致您出现错误正在看。

You don't see them in the first version because you didn't include those headers.您在第一个版本中看不到它们,因为您没有包含这些标题。

Simple way to check: Try renaming the enum identifiers.简单的检查方法:尝试重命名枚举标识符。

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

Perhaps on of the enumerators like SUCCESS was already previously defined.也许之前已经定义了一个像SUCCESS这样的枚举器。 Try using SUCCESS_TEST or something to verify this.尝试使用 SUCCESS_TEST 或其他东西来验证这一点。

Edit编辑

Another way to verify this is to compile the code with the -E option, this shows the preprocessor output.验证这一点的另一种方法是使用-E选项编译代码,这显示了预处理器 output。 For example: gcc -E main.cpp例如: gcc -E main.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到有关模板类的错误 - I'm getting errors concerning my template class 我不太明白为什么我遇到关于向量的错误 - I dont quite understand why i am getting this error concerning vectors 我正在尝试创建一个枚举,但我得到“标识符预期错误” - I'm trying to create an enum but I get “identifier expected error” 我在变量上得到3个C4703错误我认为我已经正确初始化了,我不确定我错过了什么 - I'm getting 3 C4703 errors on variables I think I've initialized correctly and I'm not sure what I'm missing 为什么这会导致运行时错误? 我想我也是用循环写的 - Why is this giving runtime error?? I think i'm write with the loops then too 我收到错误消息:Missing; *之前,但我没有错过 - I'm getting error: Missing ; before *, but I'm not missing it 我想我已经覆盖了一个虚方法,但我仍然得到:“X必须实现继承的纯虚方法Y” - I think I've overridden a virtual method but I'm still getting: “X must implement the inherited pure virtual method Y” 我从以下代码中收到段错误。 我认为这与我的函数调用有关? - I'm getting a seg fault from the following code. I think it has something to do with my function call? 我在这里遇到链接错误,为什么? - I'm getting a linking error here, why is that? 尽管我正在防止QImage出现错误 - QImage is getting an error eventhough I'm preventing it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM