将模板typename传递给嵌套类的构造函数

时间:2014-05-23 11:51:04

标签: c++ class templates constructor nested

我正在编写一个预测校正器数值求解器。我编写了一个工作循环数组来跟踪我函数的先前值。

#include <cmath>
// Circular array
// this is fixed sized container to hold the last n update of a function
// written by Keivan Moradi 2014
template <typename T>
class carray
{
    public:
        carray(int s)
        {
            size = exp2(ceil(log2(s)));
            array =  new T[size];
            SizeNegOne = size-1;
            head = SizeNegOne;
        }
        void initialize(T n)
        {
            for (head=0; head<size; head++)
                array[head]=n;
            head = SizeNegOne;
        }
        void update(T h)
        {
            // bitwise modulus:
            // if "size" is in power of 2:
            //(head+1) & SizeNegOne = (head+1) % size
            // in contrast to modulus, this method is guaranteed to get positive values
            head = (head+1) & SizeNegOne;
            array[head]=h;
        }
        T operator[](int index)
        {
            // bitwise modulus:
            // if "size" is in power of 2:
            // (head + index) & SizeNegOne = (head + index) % size
            // in contrast to modulus, this method is guaranteed to get positive values
            return array[(head + index) & SizeNegOne];
        }
        ~carray()
        {
            delete [] array;
        }
    protected:
    private:
        T *array;
        int size, SizeNegOne, head;
};

以下代码显示了此代码的工作方式:

int main()
{
    carray<float> phi(3);
    phi.initialize(-64);
    std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;

    phi.update(6.1);
    std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;

    phi.update(7.1);
    std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;

    phi.update(8.1);
    std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;

    phi.update(9.1);
    std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;

    phi.update(10.1);
    std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;

    return 0;
}

现在我想将这个类嵌入预测器类中,以便我可以使用它:

int main()
{
    predictor<float> phi(4);
    phi.update(10);
    phi.update(11);
    phi.update(12);
    phi.update(13);
    std::cout<<phi.predict2ndOrder()<<std::endl;
}

此代码显示我失败的最佳尝试:

#include <cmath>
template <typename T>
class predictor
{
    public:
        predictor(int s)
        {
            size = s;
        }
        void update(T a)
        {
            f.update(a);
        }
        T predict2ndOrder()
        {
            return f[0] + (3/2*(f[0]-f[-1])-1/2*(f[-1]-f[-2]));
        }
    private:
        int size;
        carray<T> f(size);
        class carray
        {
            public:
                carray(int s)
                {
                    size = exp2(ceil(log2(s)));
                    array =  new T[size];
                    SizeNegOne = size-1;
                    head = SizeNegOne;
                }
                ~carray()
                {
                    delete [] array;
                }
                void initialize(T n)
                {
                    for (head=0; head<size; head++)
                        array[head]=n;
                    head = SizeNegOne;
                }
                void update(T h)
                {
                    head = (head+1) & SizeNegOne;
                    array[head]=h;
                }
                T operator[](int index)
                {
                    return array[(head + index) & SizeNegOne];
                }
            private:
                T *array;
                int size, SizeNegOne, head;
        };
};

请您告诉我如何解决这个问题?我是一名新的c ++程序员,所以请放轻松。 ;)

1 个答案:

答案 0 :(得分:0)

示例代码中有一些拼写错误,我不会覆盖,但是将T类型从预测变量转移到carray非常简单。这是一种可能性:将carray声明为模板类,就像在第一个代码片段中所做的那样。要使其余工作正常,请更正拼写错误,将carray和init size的类定义下面的size和f的声明移动到预测器构造函数中的正确顺序中作为初始化列表。

这是修改后的代码,它在VS2010中为我编写了很好的内容:

#include <cmath>
using namespace std;

template <typename T>
class predictor
{
public:
   predictor(int s)
      : size(s)
      , f(size)
   {
   }
   void update(int a)
   {
      f.update(a);
   }
   T predict2ndOrder(float deltaT)
   {
      return f[0] + deltaT*(3/4*(f[0]-f[1]-1/2*(f[1]-f[2])));
   }
private:
   template <typename S>
   class carray
   {
   public:
      carray(int s)
      {
         size = exp(ceil(log((S)s)));
         array =  new S[size];
         SizeNegOne = size-1;
         head = SizeNegOne;
      }
      ~carray()
      {
         delete [] array;
      }
      void initialize(S n)
      {
         for (head=0; head<size; head++)
            array[head]=n;
         head = SizeNegOne;
      }
      void update(S h)
      {
         head = (head+1) & SizeNegOne;
         array[head]=h;
      }
      S operator[](int index)
      {
         return array[(head + index) & SizeNegOne];
      }
   private:
      S *array;
      int size, SizeNegOne, head;
   };

   int size;
   carray<T> f;
};