bfjc 发表于 2023-6-27 14:00:38

递归调用出错,error #7121

      type octree_node
      type(point), dimension(:), pointer :: points
      type(octree_node), dimension(:), pointer :: children
      real :: x_min, x_max, y_min, y_max, z_min, z_max
      integer :: num_points
      end type octree_node

      Contains
      recursive subroutine build_octree(node, points, x_min, x_max, &
          y_min, y_max, z_min, z_max)
      type(octree_node), pointer :: node
      type(point), dimension(:), pointer :: points
      real, intent(in) :: x_min, x_max, y_min, y_max, z_min, z_max
      integer :: i, j, k, num_points, num_children
      real :: x_mid, y_mid, z_mid

      do k = 0, 1
      do j = 0, 1
          do i = 0, 1
            num_children = num_children + 1
            allocate(node%children(num_children))
            call build_octree(node%children(num_children), points,&                     !这里报错,error #7121: A ptr dummy may only be argument associated with a ptr, and this array element or section does not inherit the POINTER attr from its parent array.   
            x_min + i*(x_mid - x_min), x_min + (i+1)*(x_mid - x_min),&
            y_min + j*(y_mid - y_min), y_min + (j+1)*(y_mid - y_min),&
            z_min + k*(z_mid - z_min), z_min + (k+1)*(z_mid - z_min))
          end do
      end do
      end do

      end subroutine build_octree这里截取了一部分问题代码,在递归调用函数时第一个变量出现了问题,如果第一个变量改为node则可以运行,是因为node%children(num_children)不是指针吗,不太明白,希望大佬们帮忙看看。

fcode 发表于 2023-6-27 14:51:15

node%children 的类型虽然为type(octree_node), dimension(:), pointer :: children 是指向数组的指针。
但 node%children(num_children) 表示 node%children 指针指向目标的第 num_children 个元素。
因为 node%children 指向的目标类型是 type(octree_node), dimension(:),所以它的第 num_children 个元素,类型为 type(octree_node) 而不是 type(octree_node), pointer

有两种修改方法,我推荐第一种
一,第11行,由 type(octree_node), pointer :: node 修改为 type(octree_node) :: node
二,build_octree 函数中增加一个指针变量 type(octree_node), pointer :: pNode,在 call build_octree(pNode 时传入,传入之前先 pNode => node%children(num_children)

由于Fortran是默认传地址的,所以除非你要在子程序中分配指针,否则没有什么必要把虚参定义为指针。

bfjc 发表于 2023-6-27 15:18:33

好的好的,谢谢刘涛。:-handshake
页: [1]
查看完整版本: 递归调用出错,error #7121