我正在嘗試用 c 中的函式實作一些抽象。
我想做模板函式,它需要兩個函式作為引數:
template <class inpOutp, class decis>
bool is_part_of_triangle(inpOutp ft_take_data,
decis ft_result){
return (ft_take_data(ft_result));
}
- 第一個
ft_take_data也是模板,并將一個函式作為引數:
template <class dec>
bool take_data(dec ft_result){
...
ft_result(cathetus_size, x_X, y_X);
...
}
- 第二個
ft_result應該是的論點ft_take_data:
int result(int cath_size, int x_X, int x_Y){
...
}
我嘗試在 main 中運行它,例如:
int main(void){
return (is_part_of_triangle(take_data, result));
}
但我有來自編譯器的錯誤:
error: no matching function for call to 'is_part_of_triangle(<unresolved overloaded function type>, int (&)(int, int, int))'
return (is_part_of_triangle(take_data, result));
main.cpp:38:7: note: candidate: template<class inpOutp, class decis> bool is_part_of_triangle(inpOutp, decis)
bool is_part_of_triangle(inpOutp ft_take_data,
^~~~~~~~~~~~~~~~~~~
main.cpp:38:7: note: template argument deduction/substitution failed:
main.cpp:49:47: note: couldn't deduce template parameter 'inpOutp'
return (is_part_of_triangle(take_data, result));
我怎樣才能實作這個方案 - 運行模板函式,引數中有兩個函式,其中一個也是模板函式(呼叫第二個):
-> 函式 1(函式 2,函式 3);
-> 在 func1 { func2(func3); }
-> 在 func2 { func3(...); }
uj5u.com熱心網友回復:
這take_data是一個模板,不是可以傳遞地址/函式指標的真實函式。為了得到一個具體的功能,模板必須被實體化。
這意味著您需要通過以下內容:
take_data<TYPE OF NON-TEMPLATE FUNCTION>
或者干脆
take_data<decltype(FUNCTION)>
這意味著您可以
return is_part_of_triangle(&take_data<int (*)(int, int, int)>, &result);
或者
return is_part_of_triangle(&take_data<decltype(result)>, &result);
uj5u.com熱心網友回復:
當take_data是一個模板函式,你必須在傳遞這個函式作為引數時指定它的模板
你可以這樣做:
typedef int(*RESULT_FUNC)(int, int, int);
return (is_part_of_triangle(&take_data<RESULT_FUNC>, &result));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515134.html
標籤:C 模板编译器错误
