二进制"operator%"的类型为"double"和"int"的无效操作数 [英] Invalid operands of type 'double' and 'int' to binary 'operator%'

查看:68
本文介绍了二进制"operator%"的类型为"double"和"int"的无效操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的控制结构类编写一个程序,并试图对其进行编译.唯一的错误,至少是编译器选择的唯一错误是对二进制'operator%'说无效的类型'double'和'int'的操作数.程序的大多数都没有包含在内,因为它太长了,并且与这个问题并没有真正的联系,至少我不相信.

I'm writing a program for my control structures class and I'm trying to compile it. The only error, at least the only error the compiler is picking up is saying invalid operands of types 'double' and 'int' to binary 'operator%'. Most of the program isn't included since it's too long and doesn't really pertain to this problem, at least I don't believe.

double maxTotal, minTotal;

cin >> maxTotal >> minTotal;

int addCalc;

static_cast<int>(maxTotal);

if(maxTotal % 2 == 1)
     addCalc = minTotal;
else
     addCalc = 0;

推荐答案

您的static_cast没有执行任何操作.您应该做的是:

Your static_cast isn't doing anything. What you should be doing is:

if(static_cast<int>(maxTotal) % 2 == 1)

C ++中的变量不能更改类型.静态强制类型转换返回强制类型值,它不会更改输入变量的类型,因此您必须直接使用它或对其进行分配.

Variables in C++ cannot change types. Static cast returns the casted value it does not change the input variable's type, so you have to use it directly or assign it.

int iMaxTotal = static_cast<int>(maxTotal);

if(iMaxTotal % 2 == 1)
    addCalc = minTotal;
else
    addCalc = 0;

这也可以.

这篇关于二进制"operator%"的类型为"double"和"int"的无效操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆