【发布时间】:2011-07-22 13:25:34
【问题描述】:
我试图创建一个模板函数,在蒙特卡洛模拟中进行一些加权采样。它在下面。 input_data 可以是静态分配的数组(即 data[33])、动态分配的数组或向量。
template <class myType>
int init_roulette_calcs(myType &input_data, int inputlength, int *(&output_cdf), int highclassix, int weight)
{
sort(input_data, input_data + inputlength); //where the error occurs
//other code:
output_cdf = new int [inputlength];
int k = 1;
for (int i = 0; i < inputlength; i++)
{
output_cdf[i] = k;
if (i+1 < highclassix) k++;
else k += weight;
}
return output_cdf[inputlength-1];
}
代码将无法编译,因为模板函数无法推断出调用排序的参数。这可能是一个愚蠢的问题,但我需要做些什么来确保排序能够正常工作?
Error 4 error C2784: 'std::_Vb_iterator<_Alloc> std::operator
+(_Alloc::difference_type,std::_Vb_iterator<_Alloc>)' : could not deduce template argument for
'std::_Vb_iterator<_Alloc>' from 'int' j:\rdm\lrgv_2011-07-21\lrgv_src\lrgv.h 398
提前感谢您的帮助。
【问题讨论】: