永发信息网

急问fortran编程问题

答案:3  悬赏:10  手机版
解决时间 2021-02-22 04:42
  • 提问者网友:轮囘Li巡影
  • 2021-02-21 19:01
program arraytest2
implicit none
integer,allocatable::a(:)
integer::b(2)=(/1,3/)
call sub(a,b)
print*,a
pause
end program

subroutine sub(a,b)
integer,intent(out),allocatable::a(:)
integer,intent(in)::b
allocate(a(size(b)))
a=b+1
end subroutine

编译出现一个错误:
Error: A dummy argument name is invalid in this context. [A]
实际上,我的源程序是要从subroutine输出一个数组,其长度是在subroutine运行过程中确定的。因此我在主程序里声明integer,allocatable::a(:),在subroutine里声明integer,intent(out),allocatable::a(:)。
因源程序较长,不详细贴出了。谢谢您的回答,任何建议都非常感激!

Error是出在subroutine里面
integer,intent(out),allocatable::a(:)
这一行。
最佳答案
  • 五星知识达人网友:胯下狙击手
  • 2021-02-21 20:25
fortran里不允许虚参是allocatable的吧
你这个程序应该在主程序call sub(a,b)之前allocate a,然后子程序里用假定形状的数组
integer,intent(out),dimension(:) :: a
全部回答
  • 1楼网友:从此江山别
  • 2021-02-21 21:58
由于数组a没有定义大小,所以不可出现在虚参里,可将a设置成全局变量,从虚参中删除,我习惯用module,更改程序如下: module adata implicit none integer,allocatable::a(:) endmodule program arraytest2 use adata implicit none integer::b(2)=(/1,3/) call sub(b) print*,a pause end program subroutine sub(b) use adada integer::b allocate(a(size(b))) a=b+1 end subroutine
  • 2楼网友:廢物販賣機
  • 2021-02-21 21:33
Fortran2003支持,估计您的编译器有点儿老。 我已在编程爱好者论坛回复了您的问题,不过论坛里的asymptotic兄回复得更完美:)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯