C++ I';我在代码的这一部分得到了这个错误,它有一个递归函数

C++ I';我在代码的这一部分得到了这个错误,它有一个递归函数,c++,recursion,C++,Recursion,编译完整个代码后,我得到了以下错误: [Error] invalid operands of types '__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}' and 'int' to binary 'operator%' 问题在于pow。它返回一个double,并且在C++中不允许使用带有非整数类型参数的%(顺便说一下,在Java中是这样) 但是,无论如何,您都不应该使用pow来平方数字 书

编译完整个代码后,我得到了以下错误:

[Error] invalid operands of types '__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}' and 'int' to binary 'operator%'

问题在于
pow
。它返回一个
double
,并且在C++中不允许使用带有非整数类型参数的
%
(顺便说一下,在Java中是这样)

但是,无论如何,您都不应该使用
pow
来平方数字

书写

auto r = rem(n/2,b,d);
return r * r % d;

情况要好得多。如果一些人喜欢使用多余的括号,他们可能更喜欢
(r*r)%d

“我在最后一行尝试了n/2到int的类型转换…”请显示代码,或者代码中包含了这一点?我不明白你所指的可能相关的内容:@formerlyknownas_463035818抱歉,我无法上传完整的代码。这太庞大了。这里有足够的答案,因为OP已经提供了编译器诊断。哦,别担心:)
auto r = rem(n/2,b,d);
return r * r % d;