简体   繁体   English

使用lambda表达式和std :: function没有匹配的函数调用错误。

[英]No matching function call error with usage of, lambda expressions and std::function.

I am trying to use this lambda expression in my code. 我试图在我的代码中使用此lambda表达式。 And looks like something's wrong with definition of functor or lambda. 似乎函子或lambda的定义有问题。

I believe the lambda expression is true. 我相信lambda表达式是正确的。 but I can't satisfy the prototype of function I defined. 但是我不能满足我定义的函数原型。

Function definition: 功能定义:

    template<typename E, typename container> 
void for_each(Iterator<E,container> begin, std::function<void(E&)> fun)
{
    do // does the given unary functor to elements 
    {  //from starting iterator 'til the end.
        fun(*begin);
        begin = begin.next();
    }while(begin.hasNext());

    fun(*begin);
}

And caller: 和来电者:

  for_each(c.iterator(), [&](E& e){add(e);});

I except achieve this function call with lambda expression. 除了用lambda表达式实现此函数调用之外,我没有。 But compiler says "error: no matching function for call to .." 但是编译器说“错误:没有匹配的函数来调用..”

lambda is not std::function , so deduction fails. lambda不是std::function ,所以推导失败。

You might simply pass generic Functor : 您可以简单地通过泛型Functor

template<typename E, typename container, typename F> 
void for_each(Iterator<E, container> begin, F&& fun)
{
    // ...
}

Issues with the code and Possible solution approaches below: 下面的代码和可能的解决方法问题:

1. A Lambda expression is not std::function , it can only be stored in an object of type std::function . 1. Lambda expression不是std::function ,只能存储在std::function类型的对象中。

Approach: (With your existing signature): 方法:(使用您现有的签名):

std::function<void()> f = []() -> void { // do something };
for_each(vec.begin(), vec.end(), f);

2. Better solution would be to use a Callable as a parameter (Go to end of the answer). 2.更好的解决方案是将Callable用作参数(转到答案的结尾)。

3. If you want to pull up your own version of for_each , why not follow the implementation that is consistent with other algorithms or for that matter with the implementation of for_each in the algorithm header. 3.如果要提取自己的for_each版本,为什么不遵循与其他算法一致的实现,或者不遵循算法标头中for_each的实现。

Does all you want and is also consistent with other standard algorithms: en.cppreference.com/algorithms/for_each : 是否满足您的所有需求,并且与其他标准算法一致: en.cppreference.com/algorithms/for_each

    template<class InputIt, class UnaryFunction>
    constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
    {
      for (; first != last; ++first) {
        f(*first);
      }
      return f;
    }

没有匹配的 function 调用 'std::vector <std.. '< div><div id="text_translate"><p> 我正在解决的问题是:我必须采用<strong>T</strong>测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: <em>string at even position {double space} string at odd position</em> (example: <em>input</em> - StackOverflow, <em>output</em> - <strong>Sakvrlw</strong> <strong>tcOefo</strong> ). 我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 然后我将 vector 的元素分配给另一个声明的 string s。</p><pre> #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T,i; cout &lt;&lt; "Enter no. of test cases: "; cin &gt;&gt; T; vector&lt;string&gt; v; vector&lt;string&gt; odd; vector&lt;string&gt; even; string str; for(int i=0; i&lt;T; i++){ cin &gt;&gt; str; v.push_back(str); } string s; for(i=0; i&lt;v.size(); i++){ s = v.at(i); for(i=0; i&lt;s.size(); i++){ if(i==0){ even.push_back(s[i]); //*This is where I am getting error*. }else if(i==1){ odd.push_back(s[i]); }else{ if(i%2==0){ even.push_back(s[i]); }else{ odd.push_back(s[i]); } } } for(i=0; i&lt;even.size(); i++){ cout &lt;&lt; even.at(i); } cout &lt;&lt; " "; for(i=0; i&lt;odd.size(); i++){ cout &lt;&lt; odd.at(i); } cout &lt;&lt; endl; even.clear(); odd.clear(); s.clear(); } return 0; }</pre><p> 在编译上面的代码时,我得到"no matching error for call std::vector..." 。 <strong><em>我到底做错了什么?</em></strong></p></div></std..> - no matching function for call to 'std::vector<std.. '

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: function and error:没有匹配函数用于调用 - std::function and error: no matching function for call to 错误:没有匹配函数来调用...(std :: string&) - error: no matching function for call to…(std::string&) 使用std :: find没有匹配的函数调用错误 - no matching function call error using std::find 错误:没有用于调用 std::thread 的匹配函数 - error: no matching function for call to std::thread 没有匹配的函数来调用&#39;std :: advance&#39;错误 - No matching function for call to 'std::advance' error std::string::replace “没有匹配的 function for call”错误 - std::string::replace “no matching function for call” error C ++ Lambda-错误:没有匹配的函数可调用 - C++ Lambda - error: no matching function for call to std :: bind“呼叫没有匹配功能” - std::bind “no matching function for call” 没有匹配的 function 调用 Std::find? - No matching function to call Std::find? 没有匹配的 function 调用 'std::vector <std.. '< div><div id="text_translate"><p> 我正在解决的问题是:我必须采用<strong>T</strong>测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: <em>string at even position {double space} string at odd position</em> (example: <em>input</em> - StackOverflow, <em>output</em> - <strong>Sakvrlw</strong> <strong>tcOefo</strong> ). 我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 然后我将 vector 的元素分配给另一个声明的 string s。</p><pre> #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T,i; cout &lt;&lt; "Enter no. of test cases: "; cin &gt;&gt; T; vector&lt;string&gt; v; vector&lt;string&gt; odd; vector&lt;string&gt; even; string str; for(int i=0; i&lt;T; i++){ cin &gt;&gt; str; v.push_back(str); } string s; for(i=0; i&lt;v.size(); i++){ s = v.at(i); for(i=0; i&lt;s.size(); i++){ if(i==0){ even.push_back(s[i]); //*This is where I am getting error*. }else if(i==1){ odd.push_back(s[i]); }else{ if(i%2==0){ even.push_back(s[i]); }else{ odd.push_back(s[i]); } } } for(i=0; i&lt;even.size(); i++){ cout &lt;&lt; even.at(i); } cout &lt;&lt; " "; for(i=0; i&lt;odd.size(); i++){ cout &lt;&lt; odd.at(i); } cout &lt;&lt; endl; even.clear(); odd.clear(); s.clear(); } return 0; }</pre><p> 在编译上面的代码时,我得到"no matching error for call std::vector..." 。 <strong><em>我到底做错了什么?</em></strong></p></div></std..> - no matching function for call to 'std::vector<std.. '
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM