C ++< complex>和< complex.h>在同一个文件 [英] C++ <complex> and <complex.h> in the same file

查看:357
本文介绍了C ++< complex>和< complex.h>在同一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的代码库,使用c ++ < complex> 头和许多 std :: complex< double> 对象。但现在我还想使用其他几个图书馆( fftw spinsfast ),它使用< complex.h> 。不幸的是,混合这两种类型的复合体似乎与gcc 4.6.1(大概是其他)不兼容。



这是一个最小的工作示例,显示错误:

  //这是我为我的各种复杂对象做的
#include< complex>

//这是FFTW / spinsfast基本上做的事情之一
externC{
#include< complex.h>
}

int main(){
std :: complex< double>(1.0,2.0);
return 0;
}

当我编译时:

 > g ++ test.cpp -o test 
test.cpp:在函数'int main()'中:
test.cpp:7:8:错误:'_Complex'之前的期望非限定ID test.cpp:7:8:error:expected';'before'_Complex'

显然,gcc正在将 std :: complex< double> 翻译成 _Complex ,这也不明确。 [这工作正常在我的macbook,它使用苹果LLVM版本5.1;这个编译器错误发生在我需要支持的集群上。]



我甚至不能确定这是从哪里来的;没有在我的gcc安装中的包含文件有_Complex - 虽然他们有_ComplexT。如何调试这种东西?



或者更有帮助的是,我如何解决这种编译器错误的方式,将工作不仅仅是一小片 gcc s?

解决方案

在C, _Complex 是用于声明复数的关键字: float _Complex 。但是,他们 #define complex _Complex 所以你可以写 float complex 看起来更好。



当然,在除了 _Complex 之外的上下文中使用名称 complex code>,例如 std :: complex ,然后扩展为 std :: _ Complex 因此,如果你在C ++中使用< complex> ,你应该摆脱这个宏:

  #include< complex.h> 
#undef complex

实际上,g ++从4.8开始支持<$ c $


$ b $

b

请注意,启用C ++ 11时,您也不会得到错误。


I have a large code base that uses the c++ <complex> header and many std::complex<double> objects. But now I also want to use a couple other libraries (fftw and spinsfast) that use <complex.h>. Unfortunately, mixing these two types of complex seems to be incompatible with gcc 4.6.1 (presumably among others).

Here's a minimal working example showing the error:

// This is what I do for my various complex objects
#include <complex>

// This is one of many things FFTW/spinsfast essentially do
extern "C" {
  #include <complex.h>
}

int main() {
  std::complex<double>(1.0,2.0);
  return 0;
}

And when I compile:

> g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:7:8: error: expected unqualified-id before ‘_Complex’
test.cpp:7:8: error: expected ‘;’ before ‘_Complex’

Evidently, gcc is translating std::complex<double> into _Complex, which somehow is also undefined. [This works fine on my macbook, which uses Apple LLVM version 5.1; this compiler error is happening on a cluster that I need to support.]

I can't even figure out where this is coming from; none of the include files in my gcc installation have "_Complex" -- though they do have "_ComplexT". How do I debug this kind of thing?

Or more helpfully, how do I solve this compiler error in a way that will work for more than just a small slice of gccs?

解决方案

In C, _Complex is a keyword used to declare complex numbers: float _Complex. However, they #define complex _Complex so you can write float complex which looks nicer.

Of course, you get in trouble when using the name complex in a context other than where you want _Complex, such as std::complex, which then expands to std::_Complex.

So if you use <complex> in C++, you should get rid of this macro:

#include <complex.h>
#undef complex

That's actually what g++ does since 4.8 to support both <complex> and <complex.h> in the same translation unit.

Note that when enabling C++11, you won't get the error either.

这篇关于C ++&lt; complex&gt;和&lt; complex.h&gt;在同一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆