我有以下代码:
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
我想使用toInt
函数并作为第二个参数进行传递,类型为 num 的参数
数 n;
toInt("2",n);
这会导致编译错误。
cannot convert parameter 2 from 'num' to 'int &'
我尝试使用强制转换:toInt("2",(num)n);
但它仍然存在问题
我该如何解决这个问题?
最佳答案
num
类型的值不是 int
,因此在传递给之前必须将其转换为临时 int
功能。临时对象不能绑定(bind)到非常量引用。
如果您想要通过int
进行转换,则必须分两步进行转换:
int temp;
toInt("2", temp);
num n = static_cast<num>(temp);
关于c++ - 枚举引用参数传输而不是 int 引用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13546215/