我想知道如何在C++中正确使用for和while循环中的指针。通常我使用C而不是C++编写。我这次使用C++ std库的唯一原因是我可以使用代码中其他数学函数所需的复数函数。编译时使用指针for conditional while/for循环会产生错误
作为赋值的一部分,我们给了下面的函数声明。我写的部分是在函数中注释的。
typedef std::complex<double> complex;
// Evaluates a polynomial using Horner's approach.
// Inputs:
// [coeffs, coeffs_end) - polynomial coefficients, ordered by descending power
// x - point of evaluation
// Outputs:
// p - value of polynomial at x
// dp - value of polynomial derivative at x
// ddp - value of polynomials second derivative at x
//
template<typename T>
inline void poly_val(T const* coeffs, T const* coeffs_end, T x, T & p, T & dp, T & ddp)
{
//MY CODE HERE
int i = 0;
const T *pnt = coeffs;
while(pnt != coeffs_end){
//Evaluate coefficients for descending powers
p += coeffs(i)*pow(x,((coeffs_end-1)-i));
pnt++;
i++;
}
}
的功能不知道数组的长度,所以我猜为停止条件指针“coeffs_end”,它指向数组“coeffs”的最后一个值。我可以在这种条件下使用指针吗? (传统上,我会喂阵列到函数的长度,但我们不能修改声明)
如果我这样做,这样一来我一直在编译时得到一个错误(我不明白):
C2064:长期敌人不能评价服用1个参数
以下行的函数:
p += coeffs(i)*pow(x,((coeffs_end-1)-i));
你到底打算让'coeffs(ⅰ)'做什么? 'coeffs'不是一种功能,是吗? – jwodder
你确定它是'敌人'而不是'coeffs'的错误吗?另外,它应该是'coeffs [i]'而不是'coeffs(i)'? – quasiverse
是的,它的意思是coeffs [i]。对不起,使用matlab太多了...... – user986875