我嘗試編譯這個簡單的例子:指針到複雜<double>罪
#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;
}
和錯誤:
$ 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;
^
爲什麼這個樣本有錯誤?如何獲得複雜案件的正確行爲?非常感謝。
複雜的正弦函數聲明:模板複合罪(const的複雜 & x);來源:http://www.cplusplus.com/ reference/complex/sin/ –
newmanc
2013-05-01 22:55:33