C++编译错误只有一个候选函数

C++ Compilation Error With only one candidate function

本文关键字:有一个 候选函数 错误 编译 C++      更新时间:2023-10-16

编译代码时,出现以下错误。如果只有一个候选人,为什么要抛出错误?。为什么它不能使用它?

错误:调用没有匹配函数

TemplateParameters::reset_template_params(
  const char [8],
  const char [11],
  std::vector<const Channel*>,
  bool,
  std::map<int, String, std::less<int>,
    std::allocator<std::pair<const int, String> > >&
)

'

注:候选人为:

void TemplateParameters::reset_template_params(
  String,
  String,
  std::vector<const Channel*>&,
  bool,
  std::map<int, String, std::less<int>,
    std::allocator<std::pair<const int, String> > >&
)

调用和候选之间有两个区别:

  • 前两个String参数。如果不存在从C字符串文字到此类的隐式转换,则该调用是不可能的。

  • vectorvector&参数。我假设你正在将一个临时向量传递给一个新创建的向量。编译器不允许这样做,因为您无法将临时引用绑定到非常量引用。使用const引用将在这里起作用。但这当然意味着不能在方法内部修改参数。

    既然你没有展示你是如何调用代码的,这当然是无聊的猜测。

您正在传递字符串文字,而您的函数需要Strings。您的String类是否有一个(非explicit)构造函数,可以用char const*调用?如果没有,那就是你的问题。

检查你的参数,Can,你给出的每一件事,都可以直接转化为参数,例如:字符串不能同时为const char[8]或const char[11],除非指定并显式转换

您的函数调用:

TemplateParameters::reset_template_params()传递了5个参数,编译器找不到具有相同参数的函数。因此出现了错误。

编译器可以找到函数TemplateParameters::reset_template_params(),但您传递的参数与编译器为函数TemplateParameters::reset_template_params()看到的函数声明不匹配。

您需要有一个TemplateParameters::reset_template_params()的重载版本,该版本的参数与您调用函数时使用的参数完全相同。

相关文章: