"Cannot assign to named constant"(重新分配一个变量)
"Cannot assign to named constant" (reassigning a variable)
我正在使用标志 f
进行一些错误检查。当我想执行另一次检查时,Fortran(或者可能是 gfortran)不会让我重新分配它的值。
integer, dimension(:,:), allocatable :: A
integer :: f, n
write (*, *) "Give an integer n > 0. n = "
read (*, IOSTAT=f) n
do while(f /= 0)
print *, "Error with input. Please try again."
read (*, IOSTAT=f) n
end do
write (*, "(a, i5)") "You have entered n = ", n
allocate(A(n), STAT=f)
if (f /= 0)
print *, "Error: not enough memory for A."
end if
注意:我认为复制粘贴可能会弄乱我的间距。
f
已被声明为整数(而不是作为参数整数):integer :: f
.
我是 Fortran 的初学者,所以我很可能犯了一些难以想象的错误!
这个错误信息令人困惑,但问题在于
if (f /= 0)
print *, "Error: not enough memory for A."
end if
应该是
if (f /= 0) then
print *, "Error: not enough memory for A."
end if
implicit none
integer :: f, n
integer, dimension(:,:), allocatable :: A
write (*, *) "Give an integer n > 0. n = "
read (*, *, IOSTAT=f) n
do while (f /= 0)
print *, "Error with input. Please try again."
read (*, IOSTAT=f) n
end do
write (*, "(a, i5)") "You have entered n = ", n
allocate(A(n,n), STAT=f)
if (f /= 0) then
print *, "Error: not enough memory for A."
!exit program. How do I do this?
end if
这似乎有效。
(1) 正如 Vladimir F 指出的那样,Fortran 需要 if (<condition>) then <stuff> endif
.
(2) 正如我在上面的评论中提到的,我应该写 allocate(A(n,n), STAT=f)
.
感谢您的帮助!这个答案只是为了完整性 - 弗拉基米尔才是真正回答问题的人。
我正在使用标志 f
进行一些错误检查。当我想执行另一次检查时,Fortran(或者可能是 gfortran)不会让我重新分配它的值。
integer, dimension(:,:), allocatable :: A
integer :: f, n
write (*, *) "Give an integer n > 0. n = "
read (*, IOSTAT=f) n
do while(f /= 0)
print *, "Error with input. Please try again."
read (*, IOSTAT=f) n
end do
write (*, "(a, i5)") "You have entered n = ", n
allocate(A(n), STAT=f)
if (f /= 0)
print *, "Error: not enough memory for A."
end if
注意:我认为复制粘贴可能会弄乱我的间距。
f
已被声明为整数(而不是作为参数整数):integer :: f
.
我是 Fortran 的初学者,所以我很可能犯了一些难以想象的错误!
这个错误信息令人困惑,但问题在于
if (f /= 0)
print *, "Error: not enough memory for A."
end if
应该是
if (f /= 0) then
print *, "Error: not enough memory for A."
end if
implicit none
integer :: f, n
integer, dimension(:,:), allocatable :: A
write (*, *) "Give an integer n > 0. n = "
read (*, *, IOSTAT=f) n
do while (f /= 0)
print *, "Error with input. Please try again."
read (*, IOSTAT=f) n
end do
write (*, "(a, i5)") "You have entered n = ", n
allocate(A(n,n), STAT=f)
if (f /= 0) then
print *, "Error: not enough memory for A."
!exit program. How do I do this?
end if
这似乎有效。
(1) 正如 Vladimir F 指出的那样,Fortran 需要 if (<condition>) then <stuff> endif
.
(2) 正如我在上面的评论中提到的,我应该写 allocate(A(n,n), STAT=f)
.
感谢您的帮助!这个答案只是为了完整性 - 弗拉基米尔才是真正回答问题的人。