找到你要的答案

Q:Fortran Modules - Variable declarations conflict in module with main program

Q:Fortran模块在主程序模块变量声明冲突

I have a FORTRAN 95 code where, in the main program, these variables are declared:

integer :: i
real(8) :: dx                               
real(8), allocatable :: X(:), Y(:), Z(:)

The following function uses these values at some point during the program:

function Funcion_ceros(var_4)           
        implicit none
        real(8), intent(in) :: var_4(2)
            real(8) :: Funcion_ceros(2)
            Funcion_ceros(1) = var_4(1) - Y(i) - dx*var_4(2)
            Funcion_ceros(2) = var_4(2) - Z(i) - dx*F(X(i + 1), var_4(1), var_4(2))
end function Funcion_ceros

If I include this function in the contains section of the main program, there is no compiling issue. However, when separating it into a module, it loses access to these variables. I attempt to specify the same variabes written above in the module aswell, and get the following error:

Symbol 'i' at (1) conflicts with symbol from  module 'Module_Name', use associated at (2).

How do I separate this function into a module, allowing at the same time its access to the variables it uses from the main program?

我有一个Fortran 95码的地方,在主程序中,这些变量的声明:

integer :: i
real(8) :: dx                               
real(8), allocatable :: X(:), Y(:), Z(:)

下面的函数在程序中的某个时刻使用这些值:

function Funcion_ceros(var_4)           
        implicit none
        real(8), intent(in) :: var_4(2)
            real(8) :: Funcion_ceros(2)
            Funcion_ceros(1) = var_4(1) - Y(i) - dx*var_4(2)
            Funcion_ceros(2) = var_4(2) - Z(i) - dx*F(X(i + 1), var_4(1), var_4(2))
end function Funcion_ceros

如果我把这个函数包含在主程序的包含部分,就没有编译问题。然而,当它分离成一个模块,它失去了访问这些变量。我试图说明上面写的模块以及相同的变量,并得到以下错误:

Symbol 'i' at (1) conflicts with symbol from  module 'Module_Name', use associated at (2).

我如何将这个函数分离成一个模块,同时允许它访问主程序使用的变量?

answer1: 回答1:

You can put the variables into the module as well, then the use statement makes them available to the main program as well.

module scope_mod
    implicit none
    character(len=4) :: a = "mod "
    character(len=4) :: b = "mod "
    character(len=4) :: c = "mod "
contains
    subroutine sub()
        implicit none
        character(len=4) :: b
        a = "sub "
        b = "sub "
        write(*, *) "in sub: A from ", a, ", B from ", b, ", C from ", c
    end subroutine sub
end module scope_mod

program scope_test
    use scope_mod
    implicit none
    a = "main"
    write(*, *) "in main: A from ", a, ", B from ", b, ", C from ", c
    call sub()
    write(*, *) "after sub: A from ", a, ", B from ", b, ", C from ", c
end program scope_test

Output:

 in main: A from main, B from mod , C from mod 
 in sub: A from sub , B from sub , C from mod 
 after sub: A from sub , B from mod , C from mod 

Update: I noticed that you said you put the variables in the module 'as well' -- If you have them in the module, they will be made available to the main program by the use statement. If you then declare another variable with the same name, it will conflict. Declare the variables only in the module, and they will be available to both.

您也可以将变量放入模块中,然后使用语句使它们在主程序中可用。

module scope_mod
    implicit none
    character(len=4) :: a = "mod "
    character(len=4) :: b = "mod "
    character(len=4) :: c = "mod "
contains
    subroutine sub()
        implicit none
        character(len=4) :: b
        a = "sub "
        b = "sub "
        write(*, *) "in sub: A from ", a, ", B from ", b, ", C from ", c
    end subroutine sub
end module scope_mod

program scope_test
    use scope_mod
    implicit none
    a = "main"
    write(*, *) "in main: A from ", a, ", B from ", b, ", C from ", c
    call sub()
    write(*, *) "after sub: A from ", a, ", B from ", b, ", C from ", c
end program scope_test

输出:

 in main: A from main, B from mod , C from mod 
 in sub: A from sub , B from sub , C from mod 
 after sub: A from sub , B from mod , C from mod 

更新:我注意到,你说你把变量的模块'以及'如果你有他们的模块,他们将提供给主程序的使用语句。如果您声明另一个变量具有相同的名称,它将冲突。仅在模块中声明变量,并且它们都可以使用。

module  fortran  declaration  structured-programming