A try to simulate a class in Fortran like in OOP-languages using gfortran

33 views Asked by At

In this code below a try to simulate a class like in object oriented programming languanges is implemented, but alas the code throws an error. Which things can be corrected to get a pretty view of this code implementing similarity of the class?

This is the code:

module Person_class  
    type :: Person  
        character(len=50) :: name  
        integer :: age  
    end type Person  
contains  
    subroutine Person_new(person, name, age)  
        type(Person), intent(out) :: person  
        character(len=50), intent(in) :: name  
        integer, intent(in) :: age  
  
        person%name = name  
        person%age = age  
    end subroutine Person_new  
  
    subroutine Person_print(person)  
        type(Person), intent(in) :: person  
  
        print *, "Name: ", person%name  
        print *, "Age: ", person%age  
    end subroutine Person_print  
end module Person_class

program untitled  
    use Person_class  
  
    type(Person) :: person1, person2  
  
    call Person_new(person1, "Alice", 25)  
    call Person_new(person2, "Bob", 30)  
  
    call Person_print(person1)  
    call Person_print(person2)  
end program

This is the thrown error:

class_sim.f90:8:21:

    8 |         type(Person), intent(out) :: person
      |                     1
Error: Derived type ‘person’ at (1) is being used before it is defined
class_sim.f90:12:16:

   12 |         person%name = name
      |                1
Error: Symbol ‘person’ at (1) has no IMPLICIT type
class_sim.f90:13:16:

   13 |         person%age = age
      |                1
Error: Symbol ‘person’ at (1) has no IMPLICIT type
class_sim.f90:17:21:

   17 |         type(Person), intent(in) :: person
      |                     1
Error: Derived type ‘person’ at (1) is being used before it is defined
class_sim.f90:19:35:

   19 |         print *, "Name: ", person%name
      |                                   1
Error: Symbol ‘person’ at (1) has no IMPLICIT type
class_sim.f90:20:34:

   20 |         print *, "Age: ", person%age
      |                                  1
Error: Symbol ‘person’ at (1) has no IMPLICIT type
class_sim.f90:25:9:

   25 |     use Person_class
      |         1
Fatal Error: Cannot open module file ‘person_class.mod’ for reading at (1): No such a file or directrory
compilation terminated.
0

There are 0 answers