我正在尝试用C ++编写一些代码,而且我在使用复数时遇到了困难。这是代码:
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
using namespace std;
using namespace std::complex_literals;
double Diff (double pos,double kvector,double amp){
std::complex <double> E_complex_field=exp(-(1i)*kvector*pos);
std::complex <double> E_complexplus_field=exp(-(1i)*kvector*(pos+amp));
std::complex <double> Derivada=(E_complexplus_field-E_complex_field)/(double)amp;
return Derivada;
}
int main()
{
cout<<fixed <<setprecision(22);
int N=100;
double PI,lambda,k_vector,Epso,Eo,alfa_complex,x,paso,Fx,d;
PI=acos(-1); lambda=500*pow(10,-9); k_vector=(1/(double)lambda); Epso=1; Eo=1; paso=pow(10,-8); d=pow(10,-4);
complex<double> phase =(1i *k_vector*x); complex<double> alfa=1.+(1i)*pow(10,-21);
complex<double> E_field=Eo*exp(phase); complex<double> Ec_field=conj(E_field);
alfa_complex=imag(alfa);
x=0;
for(int i=0;i<N;i++){
Fx= (0.5)*real(Epso*pow(Eo,2)*alfa*E_field*Diff(x,k_vector,paso));
x=x+d;
}
}
代码中的错误如下:
Der_num.cpp:13:8: error: cannot convert ‘std::complex<double>’ to ‘double’ in return.
我如何获得Derivada的价值?