Thomas
Thomas

Reputation: 95

Fortran derived type constructor defined though C function

I want to define a derived type constructor through a C function. In the following example, I managed to defined a overloaded addition operator through a C interface. Altough the syntax is quite similar, the definition of the constructor fails under gfortran 4.9 with the following error:

test.f90:7.52:

    module procedure my_module_fortran_new_my_double
                                                    1
Error: 'my_module_fortran_new_my_double' at (1) is not a module procedure

After some time spend in googling or looking at previous post at stack overflow, I did not find any solution. I would be very pleased if someone could tell me what triggers this error and how to correct it.

Here is the source code of my module:

module my_module
  use iso_c_binding
  type, bind(c) :: my_double
     real(c_double) :: x
  end type my_double
  interface my_double
    module procedure my_module_fortran_new_my_double
  end interface
  interface operator (+)
     module procedure my_module_fortran_add
  end interface operator (+)
  interface
     type(my_double) function my_module_fortran_new_my_double (v) bind ( c )
       use iso_c_binding
       import :: my_double
       real (c_double), intent(in) :: v
     end function my_module_fortran_new_my_double
     type(my_double) function my_module_fortran_add (v1,v2) bind ( c )
       use iso_c_binding
       import :: my_double
       type (my_double), intent(in) :: v1,v2
     end function my_module_fortran_add
  end interface
end module my_module

Upvotes: 1

Views: 125

Answers (1)

Steve Lionel
Steve Lionel

Reputation: 7267

An external procedure isn't a module procedure. I think this is what you want:

module my_module
  use iso_c_binding
  type, bind(c) :: my_double
     real(c_double) :: x
  end type my_double

    interface
       type(my_double) function my_module_fortran_new_my_double (v) bind ( c )
         use iso_c_binding
         import :: my_double
         real (c_double), intent(in) :: v
       end function my_module_fortran_new_my_double
       type(my_double) function my_module_fortran_add (v1,v2) bind ( c )
         use iso_c_binding
         import :: my_double
         type (my_double), intent(in) :: v1,v2
       end function my_module_fortran_add
  end interface

  interface my_double
   procedure my_module_fortran_new_my_double
  end interface
  interface operator (+)
    procedure :: my_module_fortran_add
  end interface operator (+)

end module my_module

Upvotes: 2

Related Questions