[英]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>
定义了SUCCESS
、 OVERFLOW
、 UNDERFLOW
、 INCONVERTIBLE
中的一个或多个,这会导致您的枚举标识符转换为“数字常量”,这反过来会导致您出现错误正在看。
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 或其他东西来验证这一点。
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.