【问题标题】:I'm getting an error concerning enum (I think)我收到有关枚举的错误(我认为)
【发布时间】:2011-06-09 07:19:30
【问题描述】:

我正在使用 c++ 通过 SSH Secure Shell 在 Solaris 机器上远程测试代码。不确定是什么版本; Solaris、c++/编译器等(不知道如何通过SSH Secure Shell查找)...

这段代码:

#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;
  }

编译和工作正常...如您所见,它将用户输入的字符串转换为 int...

但是当这样修改时:

#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;
  }

我得到以下编译错误:

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

我查找了拼写错误,尝试移动枚举行的位置...我不知道到底发生了什么。

非常感谢您对此问题的任何帮助!

【问题讨论】:

  • 给我们指出您发布的代码中的行号,我们在这里看不到行号...
  • 你能告诉我们第 16 行在哪里吗?
  • 这是第 16 行:枚举 STR_TO_INT_STATUS { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };

标签: c++ enums


【解决方案1】:

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

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

这当然不是有效的语法。

【讨论】:

    【解决方案2】:

    我认为是以下行:

    int size_of( int );
    

    ...应该更像:

    int sizeOfInt = size_of( int );
    

    编辑

    我刚刚在我的机器上编译了它,这是你的 OVERFLOW 定义...#define 是邪恶的!

    试试这个:

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

    【讨论】:

    • 这是我的函数 size_of() 的原型
    • 是的,不知道它们也被称为前向声明!
    • 太棒了!!!感谢大家的快速回复!我是新来的,不确定要检查哪个答案...有多个答案可以帮助我解决问题。我可以检查所有有助于帮助您所有代表的答案吗?
    【解决方案3】:

    我的猜测是&lt;limits&gt;&lt;cmath&gt; 定义了SUCCESSOVERFLOWUNDERFLOWINCONVERTIBLE 中的一个或多个,这会导致您的枚举标识符被转换为“数字常量” ",这反过来会导致您看到的错误。

    您在第一个版本中看不到它们,因为您没有包含这些标题。

    简单的检查方法:尝试重命名枚举标识符。

    【讨论】:

      【解决方案4】:
      enum STR_TO_INT_STATUS { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };
      

      也许像SUCCESS 这样的枚举数之前已经定义了。尝试使用 SUCCESS_TEST 或其他方法来验证这一点。

      编辑

      验证这一点的另一种方法是使用-E 选项编译代码,这会显示预处理器的输出。例如:gcc -E main.cpp

      【讨论】:

        猜你喜欢
        • 2022-11-07
        • 1970-01-01
        • 2013-12-23
        • 2014-06-20
        • 2015-06-15
        • 1970-01-01
        • 1970-01-01
        • 2018-10-15
        相关资源
        最近更新 更多