2013-05-01 29 views
0

我嘗試編譯這個簡單的例子:指針到複雜<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; 
     ^

爲什麼這個樣本有錯誤?如何獲得複雜案件的正確行爲?非常感謝。

+3

複雜的正弦函數聲明:模板複合罪(const的複雜 & x);來源:http://www.cplusplus.com/ reference/complex/sin/ – newmanc 2013-05-01 22:55:33

回答

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

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

c_sin var2 = std::sin; 
+0

抱歉,我與此修復程序有類似的錯誤: main.cpp:19:15:error:無法從'double(*)(double)throw()'轉換爲'c_sin {aka std :: complex (*)(const std :: complex &)}'[-fpermissive] c_sin var2 = &sin; – newmanc 2013-05-01 23:03:57

+1

cause 'sin'是'math.h'中的一個,它不處理複雜信息,請參閱更新 – yngccc 2013-05-01 23:15:49

+0

非常感謝! – newmanc 2013-05-01 23:22:20