简体   繁体   English

std :: function and error:没有匹配函数用于调用

[英]std::function and error: no matching function for call to

I am calling a template based function which shares a type between a function and structure. 我正在调用一个基于模板的函数,它在函数和结构之间共享一个类型。 What is wrong with this code? 这段代码有什么问题? why do I receive error when I compile it? 编译时为什么会收到错误?

test.cpp TEST.CPP

#include <functional>
#include <iostream>

template<typename T>
struct mystruct
{
    T variable;
};

int myfunc(int x)
{
    return 2*x;
}

template<typename T>
T calculate(
    mystruct<T> custom_struct,
    std::function<T(T)> custom_func)
{
    return custom_func(custom_struct.variable);
}

int main()
{
    mystruct<int> A;
    A.variable=6;
    std::cout<<calculate(A,myfunc)<<std::endl;
    return 0;
}

Compiler results: 编译结果:

test.cpp:25:31: error: no matching function for call to ‘calculate(mystruct<int>&, int (&)(int))’
  std::cout<<calculate(A,myfunc)<<std::endl;
                               ^

There is no reason to use the std::function wrapper. 没有理由使用std::function包装器。 Instead use a general template parameter F 而是使用通用模板参数F

template<typename T, class F>
T calculate(
    mystruct<T> custom_struct,
    F custom_func)
{
    return custom_func(custom_struct.variable);
}

Live Example 实例

Note that you also forgot to access the variable member at the call site. 请注意,您也忘记了在呼叫站点访问variable成员。 Since you are doing generic programming here, you also want the return type to be equal to T , or even auto (C++14, for C++11 you want probably use decltype but that is too much repetition). 既然你在这里进行泛型编程,你也希望返回类型等于T ,甚至是auto (C ++ 14,对于C ++ 11你可能想要使用decltype但重复次数太多了)。

Your code is a bit messy, but theres always a solution. 你的代码有点乱,但总是一个解决方案。

#include <functional>
#include <iostream>

template<typename T>
struct mystruct
{
    T variable;
};

const int myfunc(const int & x)
{
    return 2*x;
}

template<typename T>
T calculate(
    mystruct<T> custom_struct,
    std::function<T(T)> custom_func)
{
    return custom_func(custom_struct.variable);
}

int main()
{
    mystruct<int> A;
    A.variable=6;
    std::cout<<calculate<int>(A,myfunc)<<std::endl;
    return 0;
}

There was just a problem with return custom_func(custom_struct) , you've to pass the variable member from that struct and add calculate<int> instead of calculate . return custom_func(custom_struct)只是一个问题,你要从该结构传递variable成员并添加calculate<int>而不是calculate

You can try/test the new code here: http://cpp.sh/33cpn 您可以在此处尝试/测试新代码: http//cpp.sh/33cpn

template<typename T>
int calculate(
    mystruct<T> custom_struct,
    std::function<T(T)> custom_func);

The compiler will attempt to deduce T from std::function<T(T)> as well as mystruct<T> , but deduction from the function pointer fails. 编译器将尝试推断Tstd::function<T(T)>以及mystruct<T>但扣从函数指针失败。 One solution would be to disable template deduction on std::function<T(T)> by making it a non-deduced context: 一种解决方案是通过使其成为非推导的上下文来禁用std::function<T(T)>上的模板推导:

template <typename T> struct identity { using type = T; };
template <typename T> using identity_t = typename identity<T>::type; 

template<typename T>
int calculate(
    mystruct<T> custom_struct,
    identity_t<std::function<T(T)>> custom_func)
{
    return custom_func(custom_struct.variable);
}

Although it makes the function signature a bit uglier, you can still have T deduced, so you can just call calculate(A,myfunc) rather than calculate<int>(A,myfunc) . 虽然它使函数签名有点丑陋,但你仍然可以推导出T ,所以你可以调用calculate(A,myfunc)而不是calculate<int>(A,myfunc)

However, in this situation you should use TemplateRex's solution as std::function comes with a bunch of overhead which you don't actually need unless you're wanting to store it somewhere. 但是,在这种情况下你应该使用TemplateRex的解决方案,因为std::function附带了一堆你实际上并不需要的开销,除非你想把它存储在某个地方。

You are wrongly passing custom_struct to custom_func in calculate() . 您错误地将custom_struct传递给calculate() custom_func

Try passing custom_struct.variable instead: 尝试传递custom_struct.variable

template<typename T>
int calculate(
    mystruct<T> custom_struct,
    std::function<T(T)> custom_func)
{
    return custom_func(custom_struct.variable);
}

Also on ideone 也在想法上

错误:没有匹配的 function 调用 'std::thread::_Invoker <std::tuple< div><div id="text_translate"><p> 我正面临线程问题。</p><p> 我试图在 Chronometer class 的线程内执行 function,包括一个 while 循环:</p><p> 这是部分代码:</p><pre> for(int i = 0; i<car_data.size();i++) { if(car_data[i]->checkArea(frame, pt1_zone, pt2_zone)) { std::thread(&Chronometer::start_chrono, car_crono[i], std::ref(chrono)); cv::rectangle(frame, car_data[i]->pt1, car_data[i]->pt2, cv::Scalar(255,0,0), 1, cv::LINE_8,0); //cv::putText(frame, "Parked", car_data[i]->pt1, cv::FONT_HERSHEY_DUPLEX, 0.9, cv::Scalar( 50, 255, 50 )); //occupancy_state = place.occupancyTrue(); //place_1.occupancy = true;</pre><p> car_crono 和 chrono 的类型</p><pre>std::vector<Chronometer*> car_crono; Chronometer chrono;</pre><p> 这是我的 class 计时器:</p><pre> class Chronometer { private: static int hour, min, sec; //std::stringstream ss; //Chronometer chrono; public: Chronometer(); static Chronometer& start_chrono(Chronometer& chrono); static Chronometer& finish_chrono(Chronometer& chrono); friend std::ostream& operator<<(std::ostream& flux, Chronometer t); Chronometer& operator=(const Chronometer& other); ~Chronometer(); };</pre><p> 对于线程,我尝试了几种参数。 最后一个:</p><pre> std::thread(&Chronometer::start_chrono, car_crono[i], std::ref(chrono));</pre><p> 我猜 ref 是必要的,但没有改变。</p><p> 这是完整的错误</p><pre>/usr/include/c++/7/thread: In instantiation of 'struct std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >': /usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*&, std::reference_wrapper<Chronometer>}]' recognizer_rtsp.cxx:348:76: required from here /usr/include/c++/7/thread:240:2: error: no matching function for call to 'std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_M_invoke(std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_Indices)' operator()() ^~~~~~~~ /usr/include/c++/7/thread:231:4: note: candidate: template<long unsigned int..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind...>) [with long unsigned int..._Ind = {_Ind...}; _Tuple = std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> >] _M_invoke(_Index_tuple<_Ind...>) ^~~~~~~~~ /usr/include/c++/7/thread:231:4: note: template argument deduction/substitution failed: /usr/include/c++/7/thread: In substitution of 'template<long unsigned int..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_M_invoke<_Ind...>(std::_Index_tuple<_Ind1...>) [with long unsigned int..._Ind = {0, 1, 2}]': /usr/include/c++/7/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >' /usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*&, std::reference_wrapper<Chronometer>}]' recognizer_rtsp.cxx:348:76: required from here /usr/include/c++/7/thread:233:29: error: no matching function for call to '__invoke(std::__tuple_element_t<0, std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >, std::__tuple_element_t<1, std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >, std::__tuple_element_t<2, std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >)' -> decltype(std::__invoke(_S_declval<_Ind>()...)) ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/7/tuple:41:0, from /usr/include/c++/7/bits/stl_map.h:63, from /usr/include/c++/7/map:61, from../alpr_utils.h:7, from recognizer_rtsp.cxx:34: /usr/include/c++/7/bits/invoke.h:89:5: note: candidate: template<class _Callable, class... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&&...) __invoke(_Callable&& __fn, _Args&&... __args) ^~~~~~~~ /usr/include/c++/7/bits/invoke.h:89:5: note: template argument deduction/substitution failed: /usr/include/c++/7/bits/invoke.h: In substitution of 'template<class _Callable, class... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*, std::reference_wrapper<Chronometer>}]': /usr/include/c++/7/thread:233:29: required by substitution of 'template<long unsigned int..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_M_invoke<_Ind...>(std::_Index_tuple<_Ind1...>) [with long unsigned int..._Ind = {0, 1, 2}]' /usr/include/c++/7/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >' /usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*&, std::reference_wrapper<Chronometer>}]' recognizer_rtsp.cxx:348:76: required from here /usr/include/c++/7/bits/invoke.h:89:5: error: no type named 'type' in 'struct std::__invoke_result<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> >'</pre><p> 我应该通过线程传递什么样的参数?</p><p> 我去了几个链接来寻找解决方案,但没有什么能解决我的问题:</p><p> <a href="https://stackoverflow.com/questions/21048906/stdthread-pass-by-reference-calls-copy-constructor" rel="nofollow noreferrer">std::thread 通过引用传递调用复制构造函数</a></p><p> <a href="https://stackoverflow.com/questions/67625058/no-matching-function-to-invoke-using-stdthread" rel="nofollow noreferrer">没有匹配的 function 调用,使用 std::thread</a></p><p> ... </p></div></std::tuple<> - error: no matching function for call to ‘std::thread::_Invoker<std::tuple

没有匹配的 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 :: 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 错误:没有匹配的 function 调用 'std::thread::_Invoker <std::tuple< div><div id="text_translate"><p> 我正面临线程问题。</p><p> 我试图在 Chronometer class 的线程内执行 function,包括一个 while 循环:</p><p> 这是部分代码:</p><pre> for(int i = 0; i<car_data.size();i++) { if(car_data[i]->checkArea(frame, pt1_zone, pt2_zone)) { std::thread(&Chronometer::start_chrono, car_crono[i], std::ref(chrono)); cv::rectangle(frame, car_data[i]->pt1, car_data[i]->pt2, cv::Scalar(255,0,0), 1, cv::LINE_8,0); //cv::putText(frame, "Parked", car_data[i]->pt1, cv::FONT_HERSHEY_DUPLEX, 0.9, cv::Scalar( 50, 255, 50 )); //occupancy_state = place.occupancyTrue(); //place_1.occupancy = true;</pre><p> car_crono 和 chrono 的类型</p><pre>std::vector<Chronometer*> car_crono; Chronometer chrono;</pre><p> 这是我的 class 计时器:</p><pre> class Chronometer { private: static int hour, min, sec; //std::stringstream ss; //Chronometer chrono; public: Chronometer(); static Chronometer& start_chrono(Chronometer& chrono); static Chronometer& finish_chrono(Chronometer& chrono); friend std::ostream& operator<<(std::ostream& flux, Chronometer t); Chronometer& operator=(const Chronometer& other); ~Chronometer(); };</pre><p> 对于线程,我尝试了几种参数。 最后一个:</p><pre> std::thread(&Chronometer::start_chrono, car_crono[i], std::ref(chrono));</pre><p> 我猜 ref 是必要的,但没有改变。</p><p> 这是完整的错误</p><pre>/usr/include/c++/7/thread: In instantiation of 'struct std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >': /usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*&, std::reference_wrapper<Chronometer>}]' recognizer_rtsp.cxx:348:76: required from here /usr/include/c++/7/thread:240:2: error: no matching function for call to 'std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_M_invoke(std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_Indices)' operator()() ^~~~~~~~ /usr/include/c++/7/thread:231:4: note: candidate: template<long unsigned int..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind...>) [with long unsigned int..._Ind = {_Ind...}; _Tuple = std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> >] _M_invoke(_Index_tuple<_Ind...>) ^~~~~~~~~ /usr/include/c++/7/thread:231:4: note: template argument deduction/substitution failed: /usr/include/c++/7/thread: In substitution of 'template<long unsigned int..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_M_invoke<_Ind...>(std::_Index_tuple<_Ind1...>) [with long unsigned int..._Ind = {0, 1, 2}]': /usr/include/c++/7/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >' /usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*&, std::reference_wrapper<Chronometer>}]' recognizer_rtsp.cxx:348:76: required from here /usr/include/c++/7/thread:233:29: error: no matching function for call to '__invoke(std::__tuple_element_t<0, std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >, std::__tuple_element_t<1, std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >, std::__tuple_element_t<2, std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >)' -> decltype(std::__invoke(_S_declval<_Ind>()...)) ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/7/tuple:41:0, from /usr/include/c++/7/bits/stl_map.h:63, from /usr/include/c++/7/map:61, from../alpr_utils.h:7, from recognizer_rtsp.cxx:34: /usr/include/c++/7/bits/invoke.h:89:5: note: candidate: template<class _Callable, class... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&&...) __invoke(_Callable&& __fn, _Args&&... __args) ^~~~~~~~ /usr/include/c++/7/bits/invoke.h:89:5: note: template argument deduction/substitution failed: /usr/include/c++/7/bits/invoke.h: In substitution of 'template<class _Callable, class... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*, std::reference_wrapper<Chronometer>}]': /usr/include/c++/7/thread:233:29: required by substitution of 'template<long unsigned int..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >::_M_invoke<_Ind...>(std::_Index_tuple<_Ind1...>) [with long unsigned int..._Ind = {0, 1, 2}]' /usr/include/c++/7/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> > >' /usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&&...) [with _Callable = Chronometer& (*)(Chronometer&); _Args = {Chronometer*&, std::reference_wrapper<Chronometer>}]' recognizer_rtsp.cxx:348:76: required from here /usr/include/c++/7/bits/invoke.h:89:5: error: no type named 'type' in 'struct std::__invoke_result<Chronometer& (*)(Chronometer&), Chronometer*, std::reference_wrapper<Chronometer> >'</pre><p> 我应该通过线程传递什么样的参数?</p><p> 我去了几个链接来寻找解决方案,但没有什么能解决我的问题:</p><p> <a href="https://stackoverflow.com/questions/21048906/stdthread-pass-by-reference-calls-copy-constructor" rel="nofollow noreferrer">std::thread 通过引用传递调用复制构造函数</a></p><p> <a href="https://stackoverflow.com/questions/67625058/no-matching-function-to-invoke-using-stdthread" rel="nofollow noreferrer">没有匹配的 function 调用,使用 std::thread</a></p><p> ... </p></div></std::tuple<> - error: no matching function for call to ‘std::thread::_Invoker<std::tuple 使用lambda表达式和std :: function没有匹配的函数调用错误。 - No matching function call error with usage of, lambda expressions and std::function. 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