继承自模板类

时间:2010-11-26 10:13:33

标签: c++ templates

            #include <iostream>
            #include <math.h>
            using namespace std;

            template<template<short,short,short,int> class Derived>
                struct AllocFactor_Core
                {

                private:
                    static long double factor_;
                    static long double calcFactor_(const short mantissa, const short exponent,const short base)
                    {

                        return mantissa * ::pow(static_cast<long double>(base),exponent);
                    }
                public:
                    static const long double getFactor()
                    {
                        return factor_;
                    }

                    void setFactor(const short mantissa,const short exponent,const short base)
                    {
                        factor_ = calcFactor_(mantissa,exponent,base);
                    }
                    void setFactor(const long double factor)
                    {
                        factor_ = factor;
                    }

                };

                template<short Mantissa, short Exponent, short Base = 10, int Tag = 0>
                struct AllocFactorScientific : private AllocFactor_Core<AllocFactorScientific>
                {
                    static short base_;
                    using AllocFactor_Core<AllocFactorScientific<Mantissa,Exponent,Base,Tag>>::getFactor;
        //I'm getting an error here saying: error: type/value mismatch at argument 1 in template 
      //parameter list for 'template<template<short int <anonymous>, short int <anonymous>, short int   
   // <anonymous>, int <anonymous> > class Derived> struct AllocFactor_Core'|  
                    };
                template<short Mantissa, short Exponent, short Base, int Tag>
                short AllocFactorScientific<Mantissa, Exponent, Base,Tag>::base_ = Base;  

请参阅代码中的评论(上面3行)。基本上会发生的是,如果我将AllocFactor_Core类作为常规非模板类,并且在使用using指令时我只提供此类的名称,后跟::和fnc名称,它可以工作,但只要我将AllocFactor_Core声明为模板我试着为它提供参数我之前得到的错误。

3 个答案:

答案 0 :(得分:4)

你的基类需要一个template template parameter,但你传递的是一个具体的类。使用例如以下是:

using AllocFactor_Core< ::AllocFactorScientific >::getFactor;

请注意,由于类名称注入,以下内容无效:

using AllocFactor_Core< AllocFactorScientific >::getFactor;

答案 1 :(得分:0)

AllocFactor_Core<AllocFactorScientific<Mantissa,Exponent,Base,Tag>>应为AllocFactor_Core<AllocFactorScientific>

答案 2 :(得分:-1)

键入嵌套模板参数时,请务必在每个&gt;之间至少包含一个空格。

C ++解析器被

弄糊涂了
AllocFactor_Core<AllocFactorScientific<Mantissa,Exponent,Base,Tag>>

应该是

AllocFactor_Core<AllocFactorScientific<Mantissa,Exponent,Base,Tag> >