c – 使用模板作为模板参数的函数
发布时间:2020-12-16 10:10:41 所属栏目:百科 来源:网络整理
导读:如何将具有模板参数的函数传递给其他函数? template class _Tvoid inc(int x) { x++;}template class FUNCvoid add(int x,FUNC f) { f(x);}int main() { int x = 0; add(x,inc); return 0;} 所以,我得到“错误:没有匹配函数来调用’添加’”. 解决方法 inc
如何将具有模板参数的函数传递给其他函数?
template <class _T> void inc(int &x) { x++; } template <class FUNC> void add(int &x,FUNC f) { f(x); } int main() { int x = 0; add(x,inc); return 0; } 所以,我得到“错误:没有匹配函数来调用’添加’”. 解决方法
inc是一个模板,而不是一个函数.你需要传递inc< int>:
template <class _T> void inc(int &x) { x++; } template <class FUNC> void add(int &x,inc<int>); return 0; } (修正了在主函数中调用f而不是添加的拼写错误.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |