Fortran 编译器错误

发布于 2024-12-08 12:17:11 字数 1647 浏览 0 评论 0原文

我正在尝试使用适用于 Linux 的 intel Fortran 编译器(版本 12.0.3)编译我的程序,但收到此错误:

buggy.f90(206): error #6219: A specification expression object must be a dummy
argument, a COMMON block object, or an object accessible through host or use 
association   [SPECTRUM]
    type(spect)                       :: spectrum
 ----------------------------------------^

This is inside a module。 type(spect) 来自另一个模块,我在麻烦模块的开头使用。这是一些代码;更多详细信息请参见下文。

module non_buggy

   implicit none

   type axis
      character(len=6) :: nucleus
      integer          :: num_data_points
      real             :: spectral_width
   end type axis

   type spect
      integer                 :: num_dim
      type(axis), allocatable :: axes(:)
      real, allocatable       :: intensity(:)
      character(len=10)       :: type = ''
   end type spect

   type(spect), target :: spectrum  ! might this be a cause of error?

   contains
   ! ...
end module non_buggy


module buggy

   use non_buggy

   implicit none

   contains

   subroutine no_problem_here()

      type(spect) :: spectrum  ! compiles beautifully
      ! ...
   end subroutine no_problem_here

   subroutine problem()
      type(spect) :: spectrum  ! does not compile, but no error if I change the variable name
      ! ...
   end subroutine problem
end module buggy

我已经阅读了该错误的含义,但我的印象并不适用于我在代码中所做的事情 - 这些行上没有指定数组边界。如果我将第二次出现的 spectrum 重命名为其他名称,错误就会消失,我想知道问题是否可能与模块 spectrum 中的模块变量 spectrum 有关。 code>non_buggy (但是即使我注释掉声明模块变量的行,错误仍然存​​在)。如果 Fortran 专家能够澄清这个问题,我将不胜感激。

预先非常感谢!

I'm trying to compile my program with the intel Fortran compiler for Linux (version 12.0.3) and I'm getting this error:

buggy.f90(206): error #6219: A specification expression object must be a dummy
argument, a COMMON block object, or an object accessible through host or use 
association   [SPECTRUM]
    type(spect)                       :: spectrum
 ----------------------------------------^

This is within a module. type(spect) comes from another module, which I use at the beginning of the troublesome module. Here's some code; more details can be found below.

module non_buggy

   implicit none

   type axis
      character(len=6) :: nucleus
      integer          :: num_data_points
      real             :: spectral_width
   end type axis

   type spect
      integer                 :: num_dim
      type(axis), allocatable :: axes(:)
      real, allocatable       :: intensity(:)
      character(len=10)       :: type = ''
   end type spect

   type(spect), target :: spectrum  ! might this be a cause of error?

   contains
   ! ...
end module non_buggy


module buggy

   use non_buggy

   implicit none

   contains

   subroutine no_problem_here()

      type(spect) :: spectrum  ! compiles beautifully
      ! ...
   end subroutine no_problem_here

   subroutine problem()
      type(spect) :: spectrum  ! does not compile, but no error if I change the variable name
      ! ...
   end subroutine problem
end module buggy

I have read about what the error means, but I have the impression that does not apply to what I'm doing in my code - no array bounds are specified on those lines. As the error goes away if I rename the second occurrence of spectrum to something else, I was wondering whether the problem might have something to do with the module variable spectrum in module non_buggy (but then the error persists, even if I comment out the line in which the module variable is declared). If a Fortran expert could clarify that issue I would be most grateful.

Thanks a lot in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-12-15 12:17:11

无法在 Linux 和 osx 上使用 ifort 11 进行重现。我没有 ifort 12,所以无法验证,但这里的要点是您正在从模块导出 spectrum,这很可能是一个坏主意。始终在模块中使用关键字private,并仅明确公开您想要公开的内容。

如果您想让频谱成为模块变量(我不明白,为什么?您期望一个且只有一个频谱吗?您确定吗?)您还可以考虑是否需要 save< /code> 关键字来保存值。

最后,您掩盖了模块变量。愚蠢的 Fortran 没有命名空间的概念。如果您将一个模块中的所有内容删除到另一个模块中,则会污染您的名称空间,并且很可能最终会遇到这些情况。在某些情况下支持子程序导入,以限制损害(尽管它变得不那么具有沟通性)。将模块变量保持在最低限度,当你这样做时,要么为它们添加一个唯一的前缀,要么干脆不使用它们,并在类似 OOP 的代码布局中规范自己。

模块应该是无状态的。您不仅可以获得灵活性,还可以减少令人头疼的多线程问题。

Can't reproduce with ifort 11 on linux and osx. I don't have ifort 12 so I can't verify, but the point here is that you are exporting spectrum from the module, which is most likely a bad idea. Always use the keyword private in your modules, and make public explicitly only what you want to let out.

If you want to make spectrum a module variable (something I don't understand, why would you? do you expect one and only one spectrum ? Are you sure about it?) you may also consider if you need a save keyword to preserve the values.

Finally, you are overshadowing a module variable. Idiotic fortran has no concept of namespacing. If you slam everything from a module within another module, you pollute your namespace, and you are very likely to end up with these cases. Favor subroutine import in some cases, to limit the damage (although it becomes less communicative). Keep your module vars to a minimum, and when you do, either prefix them with a unique prefix, or simply don't use them and discipline yourself in OOP-like layouting of your code.

Modules should be stateless. You gain both in flexibility and reduced multithreading headaches.

归途 2024-12-15 12:17:11

我在代码示例中没有看到任何错误,即使声明与全局变量(模块 non_buggy 上的变量)具有相同名称(频谱)的局部变量(在例程 no_problem_here 和 Problem 中)很容易出错。

你使用什么编译器?我使用 ifort 11.1 和 gfortran 4.7.0 毫无问题地编译了您的示例(只是注释模块 non_buggy 的关键字 CONTAINS,因为 ifort 抱怨“contains”和“end module”之间没有指令)。

I don't see any error in you code sample, even if it is error prone to declare local variables (within the routines no_problem_here and problem) having the same name (spectrum) as a global variable (the one on the module non_buggy).

What compiler are you using ? I compiled your sample with ifort 11.1 and gfortran 4.7.0 without trouble (just in commenting the keyword CONTAINS of the module non_buggy because ifort complains that there is no instruction between "contains" and "end module").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文