我正在使用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
我一直在寻找拼写错误,尝试移动枚举线的位置......我不知道世界上发生了什么。
对此问题的任何帮助都将非常感激!
答案 0 :(得分:23)
cmath
的包含将预处理器常量OVERFLOW
定义为3,将UNDERFLOW
定义为4.因此声明枚举的行变为(如果没有其他常量):
enum STR_TO_INT_STATUS { SUCCESS, 3, 4, INCONVERTIBLE };
当然是无效的语法。
答案 1 :(得分:3)
我认为这是以下几行:
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};
答案 2 :(得分:0)
我的猜测是<limits>
或<cmath>
定义了SUCCESS
,OVERFLOW
,UNDERFLOW
,INCONVERTIBLE
中的一个或多个,这会导致您的枚举标识符转换为“数字常量”,这反过来会导致您看到的错误。
您在第一个版本中没有看到它们,因为您没有包含这些标题。
简单的检查方法:尝试重命名枚举标识符。
答案 3 :(得分:0)
enum STR_TO_INT_STATUS { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };
也许已经定义了SUCCESS
之类的枚举器。尝试使用SUCCESS_TEST或其他东西来验证这一点。
验证这一点的另一种方法是使用-E
选项编译代码,这显示了预处理器输出。例如:gcc -E main.cpp