"太多的模板参数列表"错误专攻一个成员函数时, [英] "too many template-parameter-lists" error when specializing a member function
问题描述
我想定义像这样一个模板类里面的一些模板成员方法:
I would like to define some template member methods inside a template class like so:
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, A aObj, B bObj) {
std::cout << tObj << ", " << aObj << ", " << bObj << ", " << std::endl;
}
但instantializing模板类时,没有关于这三个参数的定义menthod一个错误:
But when instantializing the template class, there is an error concerning the three-argument menthod definition:
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"
能否请你点什么,我做错了什么?为什么(第2)方法是确定的,但在(3)导致编译时错误?
Could you please point what I am doing wrong? Why the (2nd) method is OK but the (3rd) causes a compile time error?
推荐答案
请阅读如何给一个模板多个参数的C ++模板教程。而不是
Please read a C++ template tutorial on how to give a template multiple parameters. Instead of
template<typename A> template<typename B> void f(A a, B b);
它做的方法是
template<typename A, typename B> void f(A a, B b);
多重模板条款重新present模板多层次(类模板 - >成员模板)。
Multiple template clauses represent multiple levels of templates (class template -> member template).
这篇关于&QUOT;太多的模板参数列表&QUOT;错误专攻一个成员函数时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!