operator overloading

Hi,

I tried to define an operator, but it didn’t work.

Please help!

Below, I place a narrowed down example, where I define a new name for multiplication of two reals.

Thanks!

Arjan





The module:

Module LibTest
IMPLICIT NONE
PRIVATE
PUBLIC :: Float

   INTEGER,PARAMETER :: Float = 4

   INTERFACE OPERATOR (.Dot.)
      MODULE PROCEDURE MapFloat
   END INTERFACE

CONTAINS

   FUNCTION MapFloat( M, y )
      REAL( Float ) MapFloat
      REAL(Float), INTENT( IN ) :: y
      REAL(Float), INTENT( IN ) :: M
      MapFloat = M*y
   END FUNCTION MapFloat

END MODULE LibTest

and the program:

PROGRAM Test
USE LibTest
IMPLICIT NONE

REAL(Float) :: M = 12.,x = 13.,y

WRITE(*,*) M
WRITE(*,*) x

y = M .Dot. x

WRITE(*,*) y

END

Hi Arjan,

You need to make the .Dot. operator public in order for it to be used by the main program. Add “PUBLIC :: OPERATOR(.Dot.)” to the top of your module.

  • Mat

Thanks!

In a different Module, I overloaded the “+” operator for a particular user-defined type. In that case I didn’t have to make the overloaded definition public. It works allright. Is that because “+” is already a known symbol for an operator?

Regards,

Arjan


ps. Can you please add an example to your Reference Manual for the assignment of a more-than-1-dimensional array?

What I mean is:

REAL :: a(2,2)
a = RESHAPE((/1.,2.,3.,4./),(/2,2/))

In analogy with the 1D array assignment, I tried a search for the string “(/”, but in vain. RESHAPE does not have an example…