Fortran Coder

查看: 2035|回复: 2
打印 上一主题 下一主题

[子程序] 递归调用出错,error #7121

[复制链接]

15

帖子

6

主题

0

精华

入门

F 币
76 元
贡献
41 点
跳转到指定楼层
楼主
发表于 2023-6-27 14:00:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[Fortran] 纯文本查看 复制代码
01type octree_node
02type(point), dimension(:), pointer :: points
03type(octree_node), dimension(:), pointer :: children
04real :: x_min, x_max, y_min, y_max, z_min, z_max
05integer :: num_points
06end type octree_node
07 
08Contains
09recursive subroutine build_octree(node, points, x_min, x_max, &
10    y_min, y_max, z_min, z_max)
11type(octree_node), pointer :: node
12type(point), dimension(:), pointer :: points
13real, intent(in) :: x_min, x_max, y_min, y_max, z_min, z_max
14integer :: i, j, k, num_points, num_children
15real :: x_mid, y_mid, z_mid
16 
17do k = 0, 1
18  do j = 0, 1
19    do i = 0, 1
20      num_children = num_children + 1
21      allocate(node%children(num_children))
22      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.   [CHILDREN]
23        x_min + i*(x_mid - x_min), x_min + (i+1)*(x_mid - x_min),&
24        y_min + j*(y_mid - y_min), y_min + (j+1)*(y_mid - y_min),&
25        z_min + k*(z_mid - z_min), z_min + (k+1)*(z_mid - z_min))
26    end do
27  end do
28end do
29 
30end subroutine build_octree
这里截取了一部分问题代码,在递归调用函数时第一个变量出现了问题,如果第一个变量改为node则可以运行,是因为node%children(num_children)不是指针吗,不太明白,希望大佬们帮忙看看。

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

2038

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1676 元
贡献
715 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 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是默认传地址的,所以除非你要在子程序中分配指针,否则没有什么必要把虚参定义为指针。

15

帖子

6

主题

0

精华

入门

F 币
76 元
贡献
41 点
板凳
 楼主| 发表于 2023-6-27 15:18:33 | 只看该作者
好的好的,谢谢刘涛。

评分

参与人数 1F 币 +1 收起 理由
fcode + 1 淡定

查看全部评分

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2025-4-28 20:08

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

快速回复 返回顶部 返回列表