- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一个简单的 Durand-Kerner求根算法,但在尝试编译代码时出现错误。这是我目前所拥有的:
#include <iostream>
#include <string> // std::stod
#include <complex>
typedef std::complex<double> cplx;
// f(x)
/************************************************/
/// This function doesn't want to be compiled, but
/// it's implemented the same way as the one below
/*************************************************/
cplx f(cplx &coeff, cplx &term, int length)
{
cplx sum {0., 0.};
for (int k=0; k<length; ++k)
{
sum += coeff[k] * pow(term[k], length - 1 - k);
}
return sum;
}
int main(int argc, char *argv[])
{
cplx *terms {new cplx[argc-1]};
cplx *initial {new cplx[argc-1]};
// Convert command-line chars to numbers
for (int k=1; k<argc; ++k)
terms[k-1] = std::stod(argv[k]);
// Initialize the seed roots
for (int k=0; k<argc-1; ++k)
initial[k] = pow(cplx(0.4, 0.9), k);
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*********************************************************/
/// This one is the same as f(x) above, but this one works
/*********************************************************/
cplx s {0., 0.};
for (int k=0; k<argc-1; ++k)
s += terms[k] * pow(initial[k], argc - 2 - k);
std::cout << s << "\n";
return 0;
}
在尝试执行多项式 f(x)(第一个函数)的计算时,我不断收到此错误:“~/Documents/cpp/Durand-Kerner/main.cpp|13|错误:不匹配对于“operator[]”(操作数类型为“cplx {aka std::complex}”和“int”)|”。
虽然错误很明显,但我不明白为什么,因为在注释“~~~”行下方实现的函数 f(x) 有效。如果我注释掉 f(x) 函数,我可以编译,但不能编译函数?
我看不出两者之间没有什么不同,也许除了“length”与显式“argc”,但“length”将作为“argc-1”传递给函数,所以我真的不明白为什么错误? (我目前正在学习 C++,如果这里很重要的话)
最佳答案
问题出现在这里:
sum += coeff[k] * pow(term[k], length - 1 - k);
但实际问题可能出在您发送引用(cplx &coeff、cplx &term)的函数的 header 中,但您希望将它们用作数组或指针。解决方法可能是这样的:
//f(x)
cplx f(cplx *coeff, cplx *term, int length)
{
cplx sum {0., 0.};
for (int k=0; k<length; ++k)
{
sum += coeff[k] * pow(term[k], length - 1 - k);
}
return sum;
}
如您所见,我已将 & 符号更改为 *。
关于C++ 复项多项式函数评估失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37911882/
我正在开发一个 Angular 应用程序并想使用 Protractor 对其进行测试。我在中继器内有一个中继器。 我这样选择第一个中继器: var firstRepeater = element.al
vector *> victimDomains; 这是什么意思? 我得到了第一颗星 ( DomainInfo* ) 但第二颗是什么? 让我们说 DomainInfo有两个属性 ID和 name .如
如果我执行这段代码: vector > idft( vector > * v) { for_each(v->begin(), v->end(), conj); 然后编译器给我以下错误: dft
我是一名优秀的程序员,十分优秀!