声明Fortran中的数组一个带参数的另一个模块的名称 [英] Declare an array in Fortran with the name of a parameter in another module

查看:623
本文介绍了声明Fortran中的数组一个带参数的另一个模块的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty了Fortran世界新。我得到了一块code,其中我理解它找到的难度。

I'm pretty new in the Fortran world. I get a piece of code, where I find difficulty in understanding it.

让我们在模块A说, VAR 被声明为整数类型的参数:

Let's say in module A, var is declared as a parameter of integer type:

integer, parameter :: var = 81

然后在另一个模块B,同名称的数组 VAR 声明:

integer :: var(2)

在这些模块中的第三个模块,用于C:

When these modules are used in a third module C:

use A
use B

会不会出现在名字冲突吗?或阵列的两个成员 VAR 将价值81?

推荐答案

有将是一个编译时错误,当您试图访问变量 VAR 在你的描述的情况下。特定的错误看起来像:

There will be a compile time error when you attempt to access the variable var in your described case. The specific error will look like:

Error: Name 'var' at (1) is an ambiguous reference to 'var' from module 'a'

是为了这些变量在全球范围?你可以声明与他们中的一个(或两个)专用所以他们被限定在模块和不污染全局范围。在这种情况下,虽然,C模块将无法使用私有变量。另一种选择是限制什么是导入了<$​​ C $ C>使用语句:

Are those variables meant to be globally scoped? You can declare one (or both) of them with private so they are scoped to the module and do not pollute the global scope. In this case though, module C would not be able to use the private variable. Another option is to limit what is imported in your use statement with:

use A, only: some_variable1, some_other_variable
use B, only: var

这将让 VAR 从B插入C的范围,将隐藏 VAR 从A

This would let var from B into the scope of C, and would hide var from A.

如果你必须有模块C中这两个变量,您可以在使用该模块重命名之一。例如:

If you have to have both variables in module C, you can rename one when you use the module. E.g.:

use A, varA => var
use B, varB => var

它可以让你的名字访问的变量VAR每个模块中的 varB

请参阅下面的例子:

module A
  integer, parameter :: var = 81
 contains
end module

module B
  integer :: var(2)
 contains
end module

module C
  use A, varA => var
  use B, varB => var
 contains
end module

program test
  use C    
  print *, varA
end program

这将打印 81

这篇关于声明Fortran中的数组一个带参数的另一个模块的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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