0

I have a problem with my code and don't really know, how to handle it. Exercise is, to have a 2D allocatable Array as a type. Then declare the values with even numbers throughout.

I get an syntax error and an unclassifiable statement, when I want to compile my code.

Can anybody guide me through, how to create an array and initialize values using a type (constructor, as I know it from java?)

Here is my source code;


    program main

        type myfield
            integer, allocatable    :: elem(:,:)
            logical                     :: initialised = .false.
            integer                    :: i, j, k
        end type myfield

        type(myfield) :: phi

        phi = myfield(1:10, 1:3)
        k = 2
        do j=1, 3
            do i=1, 10
                phi(i, j) = k
                k = 2 * k
            end do
        end do

    end program main

ERROR MESSAGE: '''

$ gfortran -c Ex.2.3.f90
Ex.2.3.f90:13:16:

  phi = myfield(1:10, 1:3)
                1
Error: Syntax error in argument list at (1)
Ex.2.3.f90:17:3:

    phi(i, j) = k
       1
Error: Unclassifiable statement at (1)

'''

Nanex1011
  • 31
  • 1
  • 4
  • 1
    What is the content of the error message? (Please edit it into the original question). – albert May 17 '20 at 16:29
  • Welcome, please take the [tour]. We do not put any tags here like newbie or solved into question titles. Any error should always be accompanied by a complete error message. – Vladimir F Героям слава May 17 '20 at 16:31
  • This - https://stackoverflow.com/questions/46570745/user-defined-constructor-for-fortran-derived-type-instance may point you towards a solution. – High Performance Mark May 17 '20 at 16:58
  • I edited the original question and added the error message. – Nanex1011 May 17 '20 at 17:19
  • HPMark, thank you, but that doesn't really lead me to a solution. I know how to handle a type (constructor) in Fortran in general. But I can't seem to get it working with arrays.. – Nanex1011 May 17 '20 at 17:19
  • If you want a multi-dimensional array constructor, then you have the approach in the linked question. If, instead, you just want to specify the shape of the component, then you should just allocate it, or provide a constructed array of right shape with dummy values. Also note that your structure constructor call is flawed in another way: you need to provide values for the non-initialized components `i`, `j` and `k`. – francescalus May 17 '20 at 17:27
  • `myfield` is a derived type scalar, not an array. You cannot write `myfield(1:10, 1:3)`, you only can use `myfield%elem(1:10, 1:3)`. The same is true for `phi`, it is not an array, you cannot write `phi(i, j)`, only `phi%elem(i, j)`. Do not forget to use `implicit none` in all your programs. – Vladimir F Героям слава May 17 '20 at 17:27

0 Answers0