אבנר יעקב
אבנר יעקב

Reputation: 117

Subroutine in Fortran don't recognize type

I have the following code

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

For some reason in the subroutine "child_cubes" the compiler say's

"/home/avner/Dropbox/final project/grav/main.f95|176|Error: Derived type ‘node’ is being used before it is defined"

although in the two first subroutine he doesn't have a problem.I don't understand the difference between the two first subroutines and this one, any idea?

Upvotes: 0

Views: 401

Answers (1)

Kai Guther
Kai Guther

Reputation: 793

Trying to compile with gfortran 4.8.5, the compiler throws the following informative error on line 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)

in addition to the error at line 49

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

So the problem is, that the both the dummy argument of the subroutine child_cubes as well as the internal pointer variable of the subroutine tree have the same name (node) as the type and thus shadow the type. Changing these names to node2 or something fixes this issue (in fact, the intel compiler is even fine with the internal pointer variable having the same name as the type, as long as you rename the dummy variable of subroutine child_cubes, so which subroutines cause trouble is compiler dependent).

Upvotes: 1

Related Questions