我正在編寫一個預測校正器數值求解器。我寫了一個工作循環數組來跟蹤我的函數以前的值。將模板類型名傳遞給嵌套類的構造函數
#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++程序員,所以請放輕鬆點。 ;)
我注意到你不遵循【三規則(http://stackoverflow.com/questions/4172722/what-是最規則的三)。 – chris
[Rule of Zero]相同(http://isocpp.org/blog/2012/11/rule-of-zero) –
所以使用'std :: vector'。 – Jarod42