0

I try to compile this short example:

#include<iostream> 
#include<math.h>
#include<complex>

typedef double (*d_sin)(double);
typedef std::complex<double> (*c_sin)(std::complex<double>);

int main(void)
{

/* case 1 */
std::cout << "sin(pi/2)=" << sin(M_PI/2) << std::endl;

/* case 2 */
d_sin var = &sin;
std::cout << "sin(pi/2)=" << (*var)(M_PI/2) << std::endl;

/* case 3 */
c_sin var2 = &sin;
std::cout << "sin(pi/2)=" << (*var2)(M_PI/2) << std::endl;

return 0;
}

and the error:

$ g++ main.cpp && ./a.out 
main.cpp: In function ‘int main()’:
main.cpp:19:15: error: invalid conversion from ‘double (*)(double)throw ()’ to ‘c_sin {aka         std::complex<double> (*)(std::complex<double>)}’ [-fpermissive]
c_sin var2 = &sin;
           ^

Why this sample has the error? How to get the correct behavior for complex case? Thanks a lot.

newmanc
  • 3
  • 3
  • 3
    Declaration of complex sin function: template complex sin (const complex& x); Source: http://www.cplusplus.com/reference/complex/sin/ – newmanc May 01 '13 at 22:55

1 Answers1

1
typedef std::complex<double> (*c_sin)(std::complex<double>);

to

typedef std::complex<double> (*c_sin)( const std::complex<double> & );

and

c_sin var2 = std::sin;
yngccc
  • 5,594
  • 2
  • 23
  • 33
  • Sorry. I have the similar error with this fix: main.cpp:19:15: error: invalid conversion from ‘double (*)(double)throw ()’ to ‘c_sin {aka std::complex (*)(const std::complex&)}’ [-fpermissive] c_sin var2 = &sin; – newmanc May 01 '13 at 23:03
  • 1
    cause `sin` is the one from `math.h` which doesn't handle complex, see update – yngccc May 01 '13 at 23:15
  • Short additional question: Why I have successful compilation with c_sin var2 = std::sin; and with c_sin var2 = &std::sin; – newmanc May 01 '13 at 23:30
  • [here](http://stackoverflow.com/questions/9552663/function-pointers-and-address-of-a-function) – yngccc May 01 '13 at 23:40