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

标签 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

我想使用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/

相关文章:

c++ - 指向数据结构成员的指针

c++ - 如何在 LLVM 传递中获取 C++ 析构函数?

c++ - 助手的静态自由函数或成员函数?

c# - 从 xaml 到 .cs 类文件中的公共(public)类的引用

c++ - 有没有办法在 C++ 中找到枚举的基数(大小)?

c++ - 为什么 NULL 被转换成 string*?

json - 如何在 TypeScript 中引用 JSON 文件?

java - Java 是否有办法将变量名称分配给 Scanner 类中的新对象?

c# - GetCustomAttribute 返回 null

Java泛型类型