“模板参数列表太多"专门化成员函数时出错

quot;too many template-parameter-listsquot; 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;
}

但是在实例化模板类的时候,关于三参数的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);

多个模板子句代表模板的多个级别(类模板->成员模板).

Multiple template clauses represent multiple levels of templates (class template -> member template).

这篇关于“模板参数列表太多"专门化成员函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)