Précédent   Forum du club des développeurs et IT Pro > Autres langages > Autres langages > Fortran
Fortran Forum d'entraide sur la programmation en Fortran. Avant de poster -> FAQ Fortran
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 19/10/2012, 11h35   #1
didge
Invité de passage
 
Inscription : octobre 2012
Messages : 2
Détails du profil
Informations forums :
Inscription : octobre 2012
Messages : 2
Points : 0
Points : 0
Par défaut Question sur procedure generique

Bonjour à tous,

J'ai écrit un module m_allocation pour surcharger la subroutine allocate (traitement des erreurs directement dans la subroutine, initialisation des valeurs...) qui ressemble à ça:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module m_allocation

private

public :: m_allocate

interface m_allocate
        module procedure m_allocate_k_i
        module procedure m_allocate_k_r
        ...
end interface

contains

logical function m_allocate_k_i(array,size,defaultValue)
        integer,dimension(:),allocatable :: array
        integer,intent(in) :: size
        integer,intent(in) :: defaultValue
...
end function m_allocate_k_i

...

end module m_allocation
Ce module fonctionne.
Mon problème : j'aurais voulu l'étendre à mes types dérivés, définis dans d'autres modules, mais définir leur fonction d'allocation dans leur module comme ceci:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module m_bar

type tBar
      character(len=8) :: name
      real,dimension(2) :: coeff
end type tBar

contains

logical function m_allocate(array,size)
      type(tBar),dimension(:),allocatable :: array
      integer,intent(in) :: size
...
end function m_allocate
J'obtiens l'erreur suivante:
error #6405: The same named entity from different modules and/or program units cannot be referenced. [M_ALLOCATE]

Je peux supprimer l'erreur en renommant ma fonction m_allocate_bar, mais je perds alors l'intérêt de procédure générique.

Comment puis je résoudre ce problème?

Merci d'avance et bonne journée
didge est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/10/2012, 12h14   #2
didge
Invité de passage
 
Inscription : octobre 2012
Messages : 2
Détails du profil
Informations forums :
Inscription : octobre 2012
Messages : 2
Points : 0
Points : 0
Je viens de trouver la solution : il est possible de resurcharger une fonction deja surchargée.

Dans mon cas cela donne:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module m_bar

type tBar
      character(len=8) :: name
      real,dimension(2) :: coeff
end type tBar

interface m_allocate
      module procedure m_allocate_bar
end interface

contains

logical function m_allocate_bar(array,size)
      type(tBar),dimension(:),allocatable :: array
      integer,intent(in) :: size
...
end function m_allocate_bar

end module m_bar
sans modifier le module m_allocation.

Bonne journée
didge est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/10/2012, 18h30   #3
Sylvain Bergeron
Modérateur
 
Inscription : août 2006
Messages : 781
Détails du profil
Informations personnelles :
Localisation : Canada

Informations forums :
Inscription : août 2006
Messages : 781
Points : 1 028
Points : 1 028
C'est exactement ce que j'allais te suggérer. Je le fais à grande échelle avec des fonctions/routines génériques comme init(), free(), read(), print(), calc, ...

J'utilise un template du genre :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module <TType>Module 

type <TType>
end type

interface init
      module procedure <TType>Init
end interface

interface free
      module procedure <TType>Free
end interface

...

contains

subroutine <TType>Init(Self)
   type (<TType>), intent(out) :: Self

end subroutine

subroutine <TType>Free(Self)
   type (<TType>), intent(inout) :: Self

end subroutine

end module <TType>
Sylvain Bergeron est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 18h34.


 
 
 
 
Partenaires

Hébergement Web