error: invalid operands of types 'int()' and 'int' to binary 'operator%'

template <typename T> template <typename VST>
void BinNode<T>::travPost(VST & visit){
    switch(rand % 2){  //报错;
        case 1: travPost_I1( this, visit); break;
        default: travPost_R( this, visit); break;
    }
}

错误信息如下:error: invalid operands of types 'int()' and 'int' to binary 'operator%'

大致意思就是说:int()和int是操作符%的无效的操作数。

我想难道是rand的类型不是整形?于是我改为了(int)rand % 2;编译通过。虽然通过了,但是毕竟是猜测,我又查了下rand的用法:

1、原型:int rand (void); //产生一个0~RAND_MAX的伪随机数,RAND_MAX的大小是依赖库的,但是标准库下保证至少为32767
2、实例:
v1 = rand() % 100;         // v1 in the range 0 to 99
v2 = rand() % 100 + 1;     // v2 in the range 1 to 100
v3 = rand() % 30 + 1985;   // v3 in the range 1985-2014


这样来看的话,也不是什么类型转换的问题,是我语法写错了尴尬,真正正确的写法应该为:

rand() % 2 //产生[0, 2)的随机数;


参考:rand



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值