不知道你想用模板Functor做什么,但这个 应该有效。 struct eval { int x(){ 返回1; } int y(){ 返回2; } }; 模板< class Functor> class ClassA { public: eval tmp; double evaluate(){ Functor functor_; return functor_(tmp); } }; 模板< class T> struct Functor { double operator()(T t _){ return t_.x()+ t_.y(); } }; int main(int argc,char * argv []) { ClassA< Functor< eval a; a.evaluate(); } Not sure what you are trying to do by that Template Functor but thisshould work. struct eval{int x(){return 1;}int y(){return 2;} }; template <class Functor>class ClassA{public:eval tmp;double evaluate(){Functor functor_;return functor_(tmp);}};template <class T>struct Functor{double operator() (T t_){return t_.x() + t_.y();} }; int main(int argc, char* argv[]){ ClassA<Functor<eval a;a.evaluate();} 或者,在一行而不是两行: 返回Functor()(); :-) Or, in one line instead of two: return Functor()();:-) 如何让''operator()''成为模板? struct Functor { 模板< class Tdouble operator()(T t_){ 返回t_.x()+ t_.y(); } }; How about making your ''operator()'' a template? struct Functor { template<class Tdouble operator()(T t_) { return t_.x() + t_.y(); } }; 是的!这很好用! =) Yes! This works fine! =) 见上文。 V - 请在通过电子邮件回复时删除大写''A' 我没有回复最热门的回复,请不要问 See above.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 但是,我还有一个问题。用户需要实现 整个仿函数,我不认为这很好。也许如果我写一个 隐藏的仿函数实现并且让用户只用 覆盖函数()(),也许??? However, I still have one problem. The user needs to implement theentire functor and I don''t think that is nice. Maybe if I write ahidden implementation of the functor and the let the user only tooverride the function ()(), perhaps??? 这篇关于带有仿函数对象的外部功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 23:58