【问题标题】:What might be causing No Match Call error when compiling C++ code?编译 C++ 代码时可能导致 No Match Call 错误的原因是什么?
【发布时间】:2014-07-30 08:10:39
【问题描述】:

编译这段代码时收到错误:

void Lift::AddDestFloors(const vector<Person>& persons,
                         vector<int>& fplan,
                         int lift_pos) {
    for (auto &person : persons) {
        int dest_flr = person.dest_floor;
        if (std::find_if(fplan.begin(), fplan.end(),
                         [dest_flr] (const Person& person) -> bool {
                             return person.dest_floor == dest_flr;
                         }) == fplan.end()) {
            InsertToMasterFlightPlan(person.dest_floor, fplan);
        }
    }
}

错误信息:

不匹配调用 '(Lift::AddDestFloors(const std::vector&,
std::vector&, int)::__lambda0) (int&)'
LiftSim 208 号线,外部位置:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h C/C++问题

我的环境是:

  • Windows 7,
  • Eclipse(开普勒)
  • Mingw 4.8.1

为什么会出现这个错误?

【问题讨论】:

  • 大概你的意思是std::find_if(persons.begin(), persons.end(), ...?您的 lambda 采用 Person 而不是 int
  • 不,不是,这只是 lambda 的位置。

标签: c++ eclipse c++11 lambda


【解决方案1】:

错误意味着(Lift::AddDestFloors(const std::vector&amp;, std::vector&amp;, int)::__lambda0) 类型的某些值以int 作为参数调用,但不能以int 值调用对象。 (例如,如果您有一个接受 bool 的 lambda,则不能使用 int 作为参数调用它)。

代码中唯一的 lambda 是 find_if 的参数:[dest_flr](const Person&amp; person)-&gt;bool {return person.dest_floor == dest_flr;})

您在fplan 上致电find_iffplan 的元素类型为 int,因此 lambda 也应该采用 int,而不是 Person

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2012-01-15
    • 1970-01-01
    相关资源
    最近更新 更多