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

Pass the template typename to the constructor of a nested class

本文关键字:嵌套 构造函数 类型      更新时间:2023-10-16

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

#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++程序员,所以别对我太苛刻了。div;)

示例代码中有一些拼写错误,我将不讨论,但是将类型T从预测器传输到数组非常简单。这里有一种可能性:像在第一个代码片段中那样,将carray声明为模板类。为了使剩下的工作,纠正拼写错误,将size和f的声明移到类定义下面,并在predictors构造函数中按正确的顺序将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;
};