Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29274 Discussions

intel f90: problem with operator overloading

vkoch
Beginner
849 Views
Hi,
we just installed the noncomercial version of the intel f90 on out Debian Linux system and I ran into trouble compiling my f90 code on it. The problem operator overloading for derived types.
In the attached sample program I have two modules, one defining a three_vector type and a method to add them.
The other defines a four_vector type and also a method to add them.
in both modules I define an interface to overload these methord with the (+) operator.
In the four_vector module I need to "use" the three_vector module for other operations not shown in the example.
This call:
use three_vector_class
is the origin of my problems.

while the addition of three_vector objects compiles and works in the main program, that of four_vector objects fails.
Obviously the overloading of the four_vector addition did not register.
Output from compiler(v 7.1) a the end.
From what I understand, this should work in f90, and BOTH the lahey f90 compiler as well as the DEC Unix compiler compile this correctly.

Does anybody have any suggestions? Did I do something non-kosher here?

Thanks
Volker
-----------------------------------------------------
#> ifc test2.f90
module THREE_VECTOR_CLASS
module function T_ADD
module FOUR_VECTOR_CLASS
module function F_ADD
program TEST
f = f1+f2 ! fails to compile
^
Error 145 at (63:test2.f90) : Non-numeric left operand of +, -, *, /, or ** operator
^
Error 145 at (63:test2.f90) : Non-numeric right operand of +, -, *, /, or ** operator

0 Kudos
5 Replies
vkoch
Beginner
849 Views
Sorry, I am new to this forum and teh attchemnt of my program failed
here it is:
module three_vector_class
  implicit none
  private :: t_add

  type three_vector
     real, dimension(1:3) ::vector
  end type three_vector

  interface operator (+)
     module procedure t_add
  end interface 

contains
  function t_add (v1,v2 ) result(v0) 
    type(three_vector), intent(in)   :: v1,v2
    type(three_vector)               :: v0
    v0%vector = v1%vector + v2%vector
  end function t_add

end module three_vector_class

!++++++++++++++++++++++++++++++++++++++++++++++

module four_vector_class
  use three_vector_class ! removing would fix the problem; but three_vector
                         ! objects are needed in the rest of the module
  
  implicit none 
  private:: f_add

  type four_vector
     real, dimension(0:3) :: vector
  end type four_vector

  interface operator (+)
     module procedure f_add
  end interface

contains
  function f_add (v1,v2 ) result(v0) 
    type(four_vector), intent(in)   :: v1,v2
    type(four_vector)               :: v0
    v0%vector = v1%vector + v2%vector
  end function f_add

  ! plus other routines involving three_vector objects

end module four_vector_class

!+++++++++++++++++++++++++++++++++++++++++++++++++++++

program test
  use three_vector_class !can be removed: same result
  use four_vector_class

  implicit none
  type (four_vector) ::  f,f1,f2
  type (three_vector) :: t,t1,t2

  t = t1+t2  ! <- works
  
  f = f1+f2  ! fails to compile

end program test

0 Kudos
tgaragou
Beginner
849 Views
Same problem here. When 2 modules overload the same operator, the overloading does not register. Solution in v7 was to have different modules for the TYPE definitions and for the OPERATORs (overloading, functions etc.)
In v8 (20031016Z) this does not work anymore and no solution has been found.

MODULE A
TYPE A
...

MODULE B
TYPE B
...

MODULE AOps
USE A
OPERATOR (*) ! does A*A etc.
....

MODULE BOps
USE A
USE B
OPERATOR (*) ! does B*B _and_ A*B
...

A*B does not "get through", even for operations that are inside module BOps (ie a function that uses the A*B)
Gives "An arithmetic or LOGICAL type is required in this context" plus "This binary operation is invalid", plus "No matching user defined OPERATOR with this given type and rank has been defined" (although _everything_ is in order with other compilers), or gives a segmentation fault, depending on the moon phase from what I can tell
0 Kudos
Steven_L_Intel1
Employee
849 Views
vkock, your program works in recent versions of the Intel 8.0 compiler.
tgaragou, you are using an old version of the compiler. Please update to a current one and see if the problem persists. If it does, please submit an example to Intel Premier Support.
0 Kudos
tgaragou
Beginner
849 Views
ifort -V gives
package ID: l_fc_p_8.0.034

on
http://www.intel.com/software/products/compilers/downloads/forlin.htm
I see the file
I_fc_p_8.0.034.tar.gz

Where is the new version?
0 Kudos
Steven_L_Intel1
Employee
849 Views
You download the current version from Premier SupportYou have to register your serial number first. Depending on the license you have, you may be limited as to how recent a version you can download. If you have an evaluation or non-commercial license, you can get the version current at the time you got your license.
Starting with the next version, we will be "refreshing" the "web store" copy (what you're looking at) several times a year. You'll still be able to use your Premier Support account to get a current version.
0 Kudos
Reply