c++ - 函数错误的参数太多,即使我有一个参数数量正确的函数

标签 c++

#include <iostream>
#include <cstdlib>

using namespace std;

unsigned int idiv_rec(unsigned int a, unsigned int b)
{
 if (b == 0) exit(EXIT_FAILURE);
 unsigned int l = 0, h = a;
 return idiv_rec(a, b, l, h);
}

unsigned int idiv_rec(  unsigned a,  unsigned b, unsigned &l, unsigned &h) {
 unsigned int m = (l + h) / 2;
 bool greater = m * b > a;
 h = greater ? m : h;
 l = greater ? l : m;

 if (h - l > 1)
  return idiv_rec(a, b, l, h);
 else
  return l;
}

它说

too many arguments to function 'unsigned int idiv_rec(unsigned int, unsigned int)'

我必须包含一些东西吗?

最佳答案

你遇到的问题是当你到达

return idiv_rec(a, b, l, h);

编译器还没有看到

unsigned int idiv_rec(  unsigned a,  unsigned b, unsigned &l, unsigned &h)

这意味着它不知道函数有 4 个参数版本。这就是为什么即使函数存在也会出现错误的原因。

你需要像这样转发声明

unsigned int idiv_rec(  unsigned a,  unsigned b, unsigned &l, unsigned &h);

之前

unsigned int idiv_rec(unsigned int a, unsigned int b)

关于c++ - 函数错误的参数太多,即使我有一个参数数量正确的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620641/

相关文章:

c++ - 在 C++ 中指向同一个内存块的数组?

c++ - 如何声明 Eigen 矩阵,然后通过嵌套循环对其进行初始化

c++ - Ubuntu:gethostbyaddr 返回 NULL 和 HOST_NOT_FOUND 错误

c++ - Visual C++ 与 QT Creator

c++ - 带有 MSG_PEEK 的 C 'recv' 不返回 -1

c++ - 使用 Tiles 检查 2D 平台游戏中的碰撞

c++ - 如何创建一个复杂的联盟?

c++ - 在 C++ 中将两个类交织在一起是一种不好的做法吗?

C++ 错误 : expected type specifier before "class name"

c++ - 我该如何做类似 if(2 variables) 的事情?