代码之家  ›  专栏  ›  技术社区  ›  אבנר יעקב

Fortran中的子例程无法识别类型

  •  0
  • אבנר יעקב  · 技术社区  · 6 年前

    我有以下代码

    module oc_tree
    
        type star
            integer :: id
            real(8) :: v(3)=0, r(3)=0
        end type
    
        type node
            real(8) :: corners(3,2)
            type(node), dimension(:), pointer :: child_nodes
            type(node), pointer :: father
            type(star), allocatable :: stars_in(:)
            real(8) :: tot_mass, center_mass(3)
            integer :: id
        end type node
    
    
        contains
    
        subroutine head_node(n2,m, stars, node1)
            real(8), intent(IN) ::m
            integer, intent(IN) :: n2
            type(star), allocatable, dimension(:), intent(in) :: stars
            real(8), parameter :: parsec = 3.085677581d16, d = 6661d3*parsec
            integer :: i
    
            type(node), intent(OUT) :: node1
    
            procedure...
    
        end subroutine head_node
    
       recursive subroutine tree(m, node1)
            type(node), intent(inout), target :: node1
            integer :: i, n, j, last_id
            real(8) :: c(3,2), r1(3)
            type(node), pointer :: node
    
            node => node1
    
    
            call child_cubes(node)
    
            procedure...
    
            end  subroutine tree
    
            subroutine child_cubes(node)
                type(node), intent(inout), target :: node
                real(8) :: x_mid, y_mid, z_mid, c(3,2)
                integer :: i
    
             procedure
    
            end subroutine child_cubes
    
    
    
    end module oc_tree
    

    由于某些原因,在子例程“child_cubes”中,编译器说

    “/home/avner/Dropbox/final project/grav/main.f95 | 176 |错误:导出 类型“node”在定义之前正在使用

    虽然在两个第一子程序中他没有问题。我不明白两个第一子程序和这个子程序的区别,知道吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Kai Guther    6 年前

    尝试使用gfortran 4.8.5编译时,编译器在第37行抛出以下信息错误

    type(node), pointer :: node
                               1   2
    Error: The type 'node' cannot be host associated at (1) because it is
           blocked by an incompatible object of the same name declared at (2)
    

    除了第49行的错误

    type(node), intent(inout), target :: node
              1
     Error: Derived type 'node' at (1) is being used before it is defined
    

    所以问题是 subroutine child_cubes 以及 subroutine tree 同名( node )作为类型,因此阴影类型。把这些名字改成 node2 或者有什么东西解决了这个问题(事实上,只要将伪变量重命名为 子例程多维数据集 ,因此哪个子例程导致故障取决于编译器)。