我想在模板类中定义一些模板成员方法,如下所示:
template <typename T> class CallSometing { public: void call (T tObj); // 1st template <typename A> void call (T tObj,A aObj); // 2nd template <typename A> template <typename B> void call (T tObj,A aObj,B bObj); // 3rd }; template <typename T> void CallSometing<T>::call (T tObj) { std::cout << tObj << "," << std::endl; } template <typename T> template <typename A> void CallSometing<T>::call (T tObj,A aObj) { std::cout << tObj << "," << aObj << std::endl; } template <typename T> template <typename A> template <typename B> void CallSometing<T>::call (T tObj,B bObj) { std::cout << tObj << "," << aObj << "," << bObj << "," << std::endl; }
CallSometing<int> caller; caller.call(12); // OK caller.call(12,13.0); // OK caller.call (12,13.0,std::string("lalala!")); // NOK - complains "error: too many template-parameter-lists"
解决方法
请阅读关于如何给模板多个参数的C模板教程.代替
template<typename A> template<typename B> void f(A a,B b);
它的做法是
template<typename A,typename B> void f(A a,B b);
多个模板子句表示多层次的模板(类模板 – >成员模板).