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

时间:2017-01-12 18:42:43

标签: 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;
}

它说

  

函数'unsigned int idiv_rec(unsigned int,unsigned int)'的参数太多

我必须提供一些内容吗?

3 个答案:

答案 0 :(得分:2)

你遇到的问题是你到达

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)

答案 1 :(得分:1)

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);   //<---here
}

在调用时,编译器尚未查看您的4参数版本。您应该在调用它之前声明4参数版本,或者在外部命名空间(在本例中为全局命名空间)或里面函数(在函数范围内,是C ++允许它)!:

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

unsigned int idiv_rec(unsigned int a, unsigned int b)
{
  //or (2)
  unsigned int idiv_rec(  unsigned a,  unsigned b, unsigned &l, unsigned &h);

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

答案 2 :(得分:0)

它被称为函数原型或函数接口是函数的声明,它指定函数的名称和类型签名(arity,参数的数据类型和返回类型),但省略了函数体。