为什么我的函数产生"invalid operands of types"错误?


我创建了两个相互引用的函数。我的round_to函数应该将两个浮点值四舍五入到最近的整数,然后我的modulo_round函数应该引用该函数并返回floor(x*y(模p。
#include <iostream>
#include <cmath>
using namespace std;
int round_to(float x, float y) {
int sum = round(x*y);

return sum;
}
int modulo_round(float x, float y, int p) {
return floor(round_to(x, y)) % p;
}
int main() {
cout << modulo_round(3.12, 3.45, 9);
}

但我收到一个错误,上面写着:error: invalid operands of types '__gnu_cxx::__enable_if<true, double>::__type' {aka 'double'} and 'int' to binary 'operator%'

有人能解释一下我的代码出了什么问题吗?

我对您的函数modulo_round做了一些更改。

int modulo_round(float x, float y, int p) {
int sum = round(x*y);
int mod = sum % p;
return mod;

希望这能解决问题!

相关内容

最新更新