Fortran 向量值函数给出总线错误
以下程序给我一个总线错误...有什么想法吗?
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
我认为这与整数,维度(n)::代表有关,但我不确定为什么会发生这种情况。
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
I think it has to do with the integer, dimension(n) :: rep
but I'm not sure why this is happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使接口显式化,以确保 Fortran 在您调用它时知道代表是什么。将您的代表函数放入单独文件中的模块中,如下所示:
然后在主程序中使用该模块以及行
Also,使用隐式none来确保所有变量都已正确声明。
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:
then use the module in your main program with the line
Also, use implicit none to make sure all your variables are declared properly.
仍然不完全是您问题的答案,但也许子例程调用而不是函数对您有用:
希望有人能为您的函数提供答案。我遇到的问题是 Fortran 不会让你做类似的事情:
Still not exactly an answer to your question, but maybe a subroutine call instead of function will work for you:
Hopefully someone will come up with an answer for your function. The problem I am having with it is that Fortran won't let you do something like: