2016-08-02 129 views
0

为什么我会收到这些错误? (我有一个clang/g++编译器。)为什么`ccos`,`csqrt`,`cpow`无法识别?

error: use of undeclared identifier 'ccos' 
error: use of undeclared identifier 'csqrt'; did you mean 'sqrt'? 
error: use of undeclared identifier 'cpow'; did you mean 'pow'? 

等等。我已经宣布我的功能为:

#include <complex> 

double ghmc1AccNormalised(double ph, double r, double t, double th){ 

    complex<double> numerator; 
    complex<double> denominator; 
    double E = exp(1); 


    numerator=-(cpow(E,t*((-2*r + r*ccos(th) + r* // ... etc 
     // what follows is 24MB of a horrible polynomial from Mathematica 
     ... 
    denominator = cpow(2,0.3333333333333333) //... etc 

return creal(numerator/denominator); 
} 

我试图确保我正确处理虚数变量。 我已经花了很长的时间寻找不同的职位,但我有以下问题:

  • 的值出来为infnan时,他们不应该
  • 我怀疑这是由于复杂的数字未被正确处理
  • This highly rated post指出,ccoscsqrt等应使用复杂的参数
  • 我已经在另外尝试了各种命名空间上面:
    • using complex;
    • using std::complex;
    • 我想也是前面加上std::complexcomplex::各功能

免责声明这是我第c/c++功能,所以请忽略琐碎的问题,除非直接关系到问题

+0

'std :: complex'是一个类,不是一个名称空间。 'cpow'等。您可以使用[reference](http://en.cppreference.com/w/cpp/numeric/complex)来查看C++函数没有前导c并且处于'std'。 – chris

+0

根据OP,我也尝试不包括该行。我将编辑帖子,因为它会分散问题 –

+0

我指的是*我也尝试将std :: complex和complex ::预先添加到每个函数* – chris

回答

3

您正在使用C标头complex.h中的功能,您不包括这些功能。但C++ header complexcos,sin,pow等提供过载*。你应该使用这些而不是C对应的。

#include <complex> 

int main() 
{ 
    std::complex<double> c{1, 2}; 
    auto c2 = pow(c, 2); 
    auto cc = sqrt(c2); 
} 

*请注意,这些都在std命名空间,但是这样是std::complex,所以参数依赖查找(ADL)允许您调用它们没有命名空间。

+0

我怎样才能使用这些?我已经遇到这个网站,并认为''需要'#include '是否需要标题? –

+0

@AlexanderMFFarlane只要使用它们。 – juanchopanza

+0

@AlexanderMcFarlane而不是'ccos()',使用'cos()'等。 –

相关问题