[英]Pass the template typename to the constructor of a nested class
I am writing a predictor corrector numerical solver. 我正在编写一个预测器校正器数值求解器。 I have written a working circular array to keep track of the previous values of my function. 我编写了一个工作循环数组,以跟踪函数的先前值。
#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;
};
The following code shows how this code is supposed to work: 以下代码显示了该代码应如何工作:
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;
}
now I want to nest this class inside a predictor class so that I can use it something like this: 现在,我想将此类嵌套在一个预测变量类中,以便可以使用如下形式:
int main()
{
predictor<float> phi(4);
phi.update(10);
phi.update(11);
phi.update(12);
phi.update(13);
std::cout<<phi.predict2ndOrder()<<std::endl;
}
This code shows my failing best attempt: 这段代码显示了我失败的最佳尝试:
#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;
};
};
Would you please let me know how to fix this? 您能告诉我如何解决此问题吗? I am a new c++ programmer so take it easy on me. 我是一名新的C ++程序员,所以请放轻松。 ;) ;)
There are some typos in the sample code which I won't cover, but transporting the type T from predictor to carray is quite simple. 示例代码中有一些错别字,我不会介绍,但是将T类型从预测变量传输到carray非常简单。 Here's one possibility: Declare carray as a template class as you did in the first code fragment. 这是一种可能:与在第一个代码片段中一样,将carray声明为模板类。 To make the rest work, correct the typos, move the declarations of size and f below the class definition for carray and init size and f as initialisation list in the correct order in predictors constructor. 要完成其余工作,请纠正错别字,将size和f的声明移到类定义的carray和init size和f的类定义下方,并以正确的顺序在预测变量构造函数中作为初始化列表。
Here's the modified code which compiles fine for me in VS2010: 这是经过修改的代码,可以在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;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.