Fortran向量值函数给出了总线错误 [英] fortran vector-valued function gives bus error
本文介绍了Fortran向量值函数给出了总线错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的程序给了我一个公交车的错误......任何想法为什么?
The following program gives me a bus error... any ideas why?
program main
integer, parameter :: n = 3
integer, dimension(n) :: out
out = rep(1,n)
print *, (out(i), i=1,n)
end program
function rep(x,n)
integer :: x
integer :: n
integer, dimension(n) :: rep
do i=1,n
rep(i) = x
enddo
end function
我认为它与 integer,dimension(n):: rep
有关,但我不确定为什么会发生这种情况。 / p>
I think it has to do with the integer, dimension(n) :: rep
but I'm not sure why this is happening.
推荐答案
您需要明确界面以确保Fortran在调用它时知道什么是rep。将你的代表函数放入一个单独的文件中,如下所示:
You need to make the interface explicit to make sure that Fortran knows what rep is when you call it. put your rep function into a module in a separate file like this:
module m_rep
contains
function rep(x,n)
implicit none
integer :: x
integer :: n
integer :: i
integer, dimension(n) :: rep
do i=1,n
rep(i) = x
enddo
end function
end module
然后使用主模块程序与行
then use the module in your main program with the line
use m_rep
另外,使用隐式无确保所有变量都已正确声明。
Also, use implicit none to make sure all your variables are declared properly.
这篇关于Fortran向量值函数给出了总线错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文