Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Module 为什么我的Fortran模块没有使我的索引变量可用?_Module_Fortran - Fatal编程技术网

Module 为什么我的Fortran模块没有使我的索引变量可用?

Module 为什么我的Fortran模块没有使我的索引变量可用?,module,fortran,Module,Fortran,这个问题是关于Fortran的。我需要通过主程序的索引I,即I=1,30。。。到一个子程序。我已经创建了一个获取索引的模块,但不知何故,它对其他子例程不可用。在下面的程序中,我想使用子例程vals4interp中的索引,我认为它应该是可用的,因为索引是公共的。然而,事实上它是不可用的 program main_prog ... do i = 1, timesteps ... call getindex(i) enddo end main_p

这个问题是关于Fortran的。我需要通过主程序的索引I,即I=1,30。。。到一个子程序。我已经创建了一个获取索引的模块,但不知何故,它对其他子例程不可用。在下面的程序中,我想使用子例程vals4interp中的索引,我认为它应该是可用的,因为索引是公共的。然而,事实上它是不可用的

program main_prog  
 ...  
 do i = 1, timesteps   
     ...   
     call getindex(i)  
 enddo  
 end main_prog  


module myindex  
 !   
 implicit none  
public  
 integer,public ::index  
contains  
! get index of loop  
subroutine getindex(index)  
integer, intent(inout)::index   

 print*,'in getindex', index  
end subroutine getindex  
!  
 subroutine vals4interp(ti,this,tvar)   
...  
 ! Here I need 'index', but it's 0 !  

call getindex(ti) !! this doesn't help... dunno why I thought it would ... 

end vals4interp
在例程getindex中,变量索引用作伪参数。但是这个变量实际上是子例程中的一个局部变量,与模块变量索引不同,尽管它们的名称相同

subroutine getindex(index)  
    integer :: index    ! <--- this is a local variable different from the module variable "index"
    print*,'in getindex', index  
end subroutine getindex
通过此修改,子例程vals4interp应打印预期值


*作为相关页面,请参见

您是否在程序的各个部分实际使用了您的模块?您是否有设置索引值的setindex?嗨,francescalus,索引值是Do循环中“i”的值。来自上一个重复问题的欢呼嗨,index的值应该是do循环中'i'的值,我只使用一次主程序中的模块来调用子例程getindex。后一个子例程中的print语句确实有我需要的值,我只是可以在我实际使用它的子例程vals4interp中看到该值。我已经投票结束了这个问题,因为它是OP早期版本的翻版,现在已经被删除了。如果可以,我会投票再次关闭它,理由是没有足够的信息来正确诊断问题。任何人如何调试代码与。。。到处都是?发布一个符合在上给出的建议的问题。我不确定建议初学者修改未通过例程参数列表的变量是否是一个好建议。@HighPerformanceMark-Hmm,对不起,我听不懂你的意思。。。您的意思是,最好通过use语句导入模块变量并直接修改它吗?在您答案中的第二个版本getindex中,变量索引被分配给,但不会通过其参数列表传递给例程;这一例行程序的运作有副作用。而且,正如我所写的,我不确定…@HighPerformanceMark我明白了,是的,通过参数列表传递数据肯定更好,代码更清晰,如果过程没有副作用,甚至更好。由于OP代码的目的似乎是在实时演化过程中执行一些数据插值,因此更清晰的做法可能是将所有历史或轨迹数据保存在内存或磁盘中,然后在可能的情况下执行插值。亲爱的roygvib,感谢您的回答,它解决了我的问题。我将尽量更加小心地处理伪论点。我试着给它重新命名过一次,但我可能错过了什么:干杯,阿德里安娜
subroutine getindex( idx )  
    integer, intent(in) :: idx   ! <--- a local variable
    print*,'in getindex', idx
    index = idx                  ! <--- set module variable "index"
end subroutine getindex