我收到一个错误关于枚举(我认为) [英] I'm getting an error concerning enum (I think)
问题描述
我通过SSH Secure Shell使用c ++在Solaris计算机上远程测试代码。不知道什么版本是什么; Solaris,c ++ /编译器等(不知道如何通过SSH安全Shell找到)...
这段代码:
#include< iostream>
#include< string>
#include< cstdlib>
#include< errno.h>
使用std :: cout;
using std :: cin;
using std :: string;
枚举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代码;
cout<< \\\
输入包含数字的字符串:;
cin>> str;
code = str_to_int(num,str.c_str(),0);
if(code == OVERFLOW || code == UNDERFLOW)
cout< \\\
该数字超出了int的范围\\\
\\\
;
else if(code == INCONVERTIBLE)
cout<< \\\
该字符串包含非数字字符\\\
\\\
;
else if(code == SUCCESS)
cout<< \\\
字符串的int版本是:< 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 return UNDERFLOW;
if(* s =='\0'|| * end!='\0')
return INCONVERTIBLE;
i = l;
return SUCCESS;
}
编译和工作正常...如你所见,
code> #include< iostream>
#include< string>
#include< cstdlib>
#include< errno.h>
#include< limits>
#include< cmath>
使用std :: cout;
using std :: cin;
using std :: string;
using std :: numeric_limits;
int size_of(int); // ------------------------------------------------ - :62
枚举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代码;
系统(clear);
cout<< \\\
int's max limit:
<< numeric_limits< int> :: max()
<< \\\
int's min limit:
<< numeric_limits< int> :: min()
<< \\\
\\\
最大int中的数字个数:
<< size_of(numeric_limits< int> :: max())
<< \\\
最小整数数字:
<< size_of(numeric_limits< int> :: min());
cout<< \\\
输入包含数字的字符串:;
cin>> str;
code = str_to_int(num,str.c_str(),0);
if(code == OVERFLOW || code == UNDERFLOW)
cout< \\\
该数字超出了int的范围\\\
\\\
;
else if(code == INCONVERTIBLE)
cout<< \\\
该字符串包含非数字字符\\\
\\\
;
else if(code == SUCCESS)
cout<< \\\
字符串的int版本是:< num<< \\\
\\\
;
cout<< 按Enter键继续...;
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)|| lreturn UNDERFLOW;
if(* s =='\0'|| * end!='\0')
return INCONVERTIBLE;
i = l;
return SUCCESS;
}
我收到以下编译错误:
int_limits.cpp:16:error:数字常量之前的期望标识符
int_limits.cpp:16:error:expected` b int_limits.cpp:16:错误:数字常量之前的期望无限额id
int_limits.cpp:16:错误:数字常量之前的预期的`,'或`;'
int_limits.cpp:16:error :'}'之前的预期声明
我已经找到拼写错误,
这个问题的任何帮助将非常感谢!
cmath
的范围是定义预处理常数 OVERFLOW
为3, UNDERFLOW
为4.因此,声明枚举的行变为(如果没有其他常量): 枚举STR_TO_INT_STATUS {SUCCESS,3,4,INCONVERTIBLE};
这当然不是有效的语法。
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 << "\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...
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):
enum STR_TO_INT_STATUS { SUCCESS, 3, 4, INCONVERTIBLE };
which, of course is not valid syntax.
这篇关于我收到一个错误关于枚举(我认为)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!