Fortran 向量值函数给出总线错误

发布于 2024-12-19 23:18:51 字数 370 浏览 0 评论 0原文

以下程序给我一个总线错误...有什么想法吗?

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-12-26 23:18:51

您需要使接口显式化,以确保 Fortran 在您调用它时知道代表是什么。将您的代表函数放入单独文件中的模块中,如下所示:

  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

然后在主程序中使用该模块以及行

  use m_rep

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:

  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.

怀念你的温柔 2024-12-26 23:18:51

仍然不完全是您问题的答案,但也许子例程调用而不是函数对您有用:

program main
implicit none
integer :: i
integer, parameter :: n = 3
integer, dimension(n) :: out

call repeat(1,3,out) 
print *, (out(i), i=1,n)

end program

subroutine repeat(x,n,y)
implicit none
integer :: i   
integer,intent(in) :: x,n
integer,dimension(n),intent(out) :: y

do i=1,n
  y(i) = x
enddo

endsubroutine

希望有人能为您的函数提供答案。我遇到的问题是 Fortran 不会让你做类似的事情:

integer,external :: rep(n)

Still not exactly an answer to your question, but maybe a subroutine call instead of function will work for you:

program main
implicit none
integer :: i
integer, parameter :: n = 3
integer, dimension(n) :: out

call repeat(1,3,out) 
print *, (out(i), i=1,n)

end program

subroutine repeat(x,n,y)
implicit none
integer :: i   
integer,intent(in) :: x,n
integer,dimension(n),intent(out) :: y

do i=1,n
  y(i) = x
enddo

endsubroutine

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:

integer,external :: rep(n)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文