C++ 枚举引用参数传输而不是int引用参数

C++ 枚举引用参数传输而不是int引用参数,c++,reference,enums,C++,Reference,Enums,我有以下代码: typedef enum {Z,O,T} num; bool toInt (str s,int& n);//<-if convert is possible converts s to integer ,puts the result in n and returns true,else returns false 我尝试使用cast:toInt(“2”,num)n)但仍然存在问题 如何解决此问题?类型为num的值不是int,因此必须将其转换为临时int,然后才能

我有以下代码:

typedef enum {Z,O,T} num;
bool toInt (str s,int& n);//<-if convert is possible converts s to integer ,puts the result in n and returns true,else returns false
我尝试使用cast:
toInt(“2”,num)n)但仍然存在问题

如何解决此问题?

类型为
num
的值不是
int
,因此必须将其转换为临时
int
,然后才能传递给函数。时间不能绑定到非常量引用


如果要通过
int
转换,必须分两步进行转换:

int temp;
toInt("2", temp);
num n = static_cast<num>(temp);
int-temp;
toInt(“2”,温度);
num n=静态铸件(温度);

我建议您添加一个新的枚举类型来签名无效的枚举,例如:

enum num {Z,O,T,Invalid=4711} ;//no need to use typedef in C++
并将签名更改为num而不是int:

bool toInt (str s, num& n)
{
 if ( s=="Z" ) n=Z; 
 else if ( s=="O" ) n=O;
 else if ( s=="T" ) n=T;
 else { n=Invalid; return false; }
 return true;
}

关于

编译器错误是什么?如果您不告诉我们,我们就帮不了您。它仍然相当模糊,但可能是因为您需要强制转换。我的意思是在使用参数的函数体内部。除了你没有发布你的函数体,我怎么知道?-1:函数名有误导性。它应该返回int,与enum的匹配应该在其他(正确命名的)函数的外部/内部完成。
bool toInt (str s, num& n)
{
 if ( s=="Z" ) n=Z; 
 else if ( s=="O" ) n=O;
 else if ( s=="T" ) n=T;
 else { n=Invalid; return false; }
 return true;
}