简体   繁体   English

复杂 <double> 在C ++中-不能使用imag(),complex(),real()

[英]Complex<double> in C++ - can't use imag(), complex(), real()

I have a problem when using functions from the complex.h I have some numbers 我在使用来自complex.h的函数时遇到问题,我有一些数字

typedef complex<double> cmplx;
cmplx N;
double x;
double y;

and now I need operations over them: 现在我需要对其进行操作:

double k = real(N);
cmplx NI = complex(x,y);
double l = imag(N);

nothing from these works. 这些作品一无所有。 What's wrong? 怎么了? I have tried to write creal, cimag, but nothing works... The compiler writes: 我尝试编写creal,cimag,但是没有任何效果...编译器写道:

error: argument list for class template "std::complex" is missing

Please for your help 请帮忙

Thanks 谢谢


EDIT 编辑

Thank you again for your help but now I have a new problem. 再次感谢您的帮助,但是现在我遇到了一个新问题。 If I have an array Arr[N] of cmplx numbers and now I need the real part of the Arr[i] I have written 如果我有一个cmplx数字数组Arr [N],而现在我需要写的Arr [i]的实部

realArray[5] = ComplexArray[5].real();

I get an error: 我收到一个错误:

error: expression must have class type

the error is the same for real() or imag() functions 该错误对于real()或imag()函数是相同的

I have a problem when using functions from the complex.h 使用来自complex.h的函数时出现问题

There is no standard header with that name. 没有带有该名称的标准标题。 I'll assume you mean <complex> ; 我假设你的意思是<complex> ; otherwise, you're including either a prehistoric or non-standard library, or the C library. 否则,您将包含史前或非标准库,或C库。 In any case, you should change to <complex> . 无论如何,您都应该更改为<complex>

The first and third lines should be fine. 第一和第三行应该没问题。 real and imag are defined both as non-member functions usable as you use them, or as member functions usable as N.real() and N.imag() . realimag既定义为在使用时可用的非成员函数,又定义为可用作N.real()N.imag()成员函数。

The second line should be one of: 第二行应为以下内容之一:

cmplx NI = cmplx(x,y);
cmplx NI = complex<double>(x,y);
cmplx NI(x,y);

either using your type alias cmplx , or the template specialisation itself, to specify the type. 使用类型别名cmplx或模板特殊化本身来指定类型。 As the error says, you can't use a template where you need a type. 如错误所示,您不能在需要类型的地方使用模板。

You want something like this: 您想要这样的东西:

double k = N.real();
cmplx NI = cmplx(x,y);
double l = N.imag();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM