2016-04-19 141 views
1

我正在使用標誌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的初學者,所以很可能我犯了一個不可思議的錯誤!

+0

當我說'allocate(A(n))'時有錯誤嗎?它應該是「分配(A(n,n))」嗎? – jamesh625

+0

這個奇怪的錯誤消息是gfortran中的一個已知錯誤:https://gcc.gnu.org/bugzilla/show_bug.cgi?id = 34325 – agentp

回答

2

此錯誤信息是混亂的,但問題是,

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 
0
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)

感謝您的幫助!這個答案只是爲了完整 - 弗拉基米爾是真正回答這個問題的人。