0

Why am I getting these errors? (I have a clang/g++ compiler.)

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'?

and so forth. I have declared my function as:

#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);
}

I am trying to ensure that I am handling the imaginary variables correctly. I have spent a long time looking at various posts but I have the following issues:

  • The values are coming out as inf and nan when they shouldn't
  • I suspected that this was due to complex numbers not being handled properly
  • This highly rated post notes that ccos, csqrt etc. should be used for complex arguments
  • I have tried various namespaces in addition to the above:
    • using complex;
    • using std::complex;
    • I tried also prepending std::complex and complex:: to each function

Disclaimer This is my first c/c++ function so please ignore trivial issues unless directly related to the question

6
  • std::complex is a class, not a namespace. cpow etc. You can use a reference to see that the C++ functions do not have the leading c and are in std.
    – chris
    Aug 2, 2016 at 21:27
  • As per the OP, I have also tried not including that line. I'll edit the post as it's going to distract form the issue Aug 2, 2016 at 21:29
  • I was referring to I tried also prepending std::complex and complex:: to each function
    – chris
    Aug 2, 2016 at 21:30
  • That "highly rated post" is about a different language (C, not C++).
    – MSalters
    Aug 3, 2016 at 12:59
  • Also, that denominator could be written as just std::cbrt(2). That's the c of "cubic root", not complex.
    – MSalters
    Aug 3, 2016 at 13:03

1 Answer 1

3

You are using functions from the C header complex.h, which you are not including. But the C++ header complex provides overloads for cos, sin, pow, etc*. You should use these instead of their C counterparts.

#include <complex>

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

* Note that these are in the std namespace, but so is std::complex, so argument dependent lookup (ADL) allows you to invoke them without the namespace.

8
  • how can I use these? I already came across this website and assumed that <complex> needed an #include <complex> is no header required? Aug 2, 2016 at 21:30
  • @AlexanderMcFarlane Just use them. Aug 2, 2016 at 21:31
  • @AlexanderMcFarlane Instead of ccos(), use cos(), etc. Aug 2, 2016 at 21:31
  • Is it literally a case of deleting #include <complex> and just using cos(), sin() etc? Aug 2, 2016 at 21:33
  • No. The complex header includes std::complex and the overloads for cos, sin, etc. Just replace ccos with cos and so on
    – Kevin
    Aug 2, 2016 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.