문제

i have a question about a loop inside a subroutine in fortran.

If i put this as subroutine, then i expect that the variable test becomes an array from 1 to 5.

p.s. type3 is defined as real, dimension(5,1)

subroutine build(test)
    type(typelist)     :: test
    do i = 1, 5
        test%type3(i)         = i 
end subroutine build

However this gives an error ;

    ||Error: Rank mismatch in array reference  (1/2)|

And when i remove the "(i)" after test%type3, it will work, but the result is 5.000 5.000 5.000 5.000 5.000. So it only assigns the value from the last loop to all entries in the array. And if i remove the %test the program does not know what type the variable test is anymore and it gives

 ||Error: Unclassifiable statement  |

Can someone tell me what i am doing wrong?

도움이 되었습니까?

해결책

Did you forget to assign with test%type3(i,1) = i?

Since type3 is a 2D array, you need two indices to assign values. When you type test%type3 = i you are assigning all elements at the same time with the same value. That is why in the end you get all 5.0.

PS. Where is the ENDDO statement?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top