我打電話給基於模板的函數共享一個函數和結構之間的類型。這段代碼有什麼問題?爲什麼我編譯時收到錯誤?std ::函數和錯誤:沒有匹配的函數調用
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;
}
編譯結果:
test.cpp:25:31: error: no matching function for call to ‘calculate(mystruct<int>&, int (&)(int))’
std::cout<<calculate(A,myfunc)<<std::endl;
^
我還要修復'int'返回類型 – user463035818
@ tobi303是,更新 – TemplateRex