【问题标题】:Compiling C code in Visual Studio 2013 with complex.h library使用 complex.h 库在 Visual Studio 2013 中编译 C 代码
【发布时间】:2014-05-24 08:36:34
【问题描述】:

http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx

C99 支持添加了 Visual Studio 2013,但我无法在“C”代码中使用 complex.h。

#include <stdio.h>
#include <complex.h>
int main(void)
{
    double complex dc1 = 3 + 2 * I;
    double complex dc2 = 4 + 5 * I;
    double complex result;

    result = dc1 + dc2;
    printf(" ??? \n", result);

    return 0;
}

我收到语法错误。

编辑:抱歉缺少部分。

error C2146: syntax error : missing ';' before identifier 'dc1'
error C2065: 'dc1' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'dc2'
error C2065: 'dc2' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'result'
error C2065: 'result' : undeclared identifier
error C2065: 'result' : undeclared identifier
error C2065: 'dc1' : undeclared identifier
error C2065: 'dc2' : undeclared identifier
error C2065: 'result' : undeclared identifier           
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: identifier "result" is undefined
IntelliSense: identifier "dc1" is undefined
IntelliSense: identifier "dc2" is undefined

【问题讨论】:

  • 你从哪里得到这些 sysntax 错误?
  • 当您发布有关错误的问题时,在问题中包含实际错误(completeunedited)非常有帮助。请编辑您的问题以包含错误。
  • 似乎对complex 的支持并没有博文告诉我们的那么完整。
  • 很遗憾 VS 对更现代的 C 版本的支持如此糟糕

标签: c visual-studio visual-studio-2013 complex-numbers


【解决方案1】:

如果有人在一年后搜索,请尝试

_Dcomplex dc1 = {3.0, 2.0};

用于变量声明。

通过查看 VS2013 的“complex.h”头文件,微软似乎决定自己实现 C 复数。您必须使用 real() 和 imag() 函数实现自己的算术运算符,即:

double real_part = real(dc1) + real(dc2);
double imag_part = imag(dc1) + imag(dc2);
_Dcomplex result = {real_part, imag_part};

【讨论】:

    【解决方案2】:

    另一种方式是定义like:

    /*_Fcomplex */  _C_float_complex a =  _FCbuild(5.0F, 1.0F);    
    printf( "z = %.1f% + .1fi\n", crealf(a), cimagf(a));    
    
    /*_Dcomplex*/ _C_double_complex b = _Cbuild(3.0, 2.0);    
    printf("z = %.1f% + .1fi\n",creal(b), cimag(b));    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多