[POO] Attribut privé non accessible depuis le constructeur
Bonjour,
Pour faire court, j'ai créé un objet "Matrix" ayant pour attributs:
- nrow : entier correspondant au nombre de lignes
- ncolumn : entier correspondant au nombre de colonnes
- array : tableau (nrow, ncolumn) contenant les valeurs de la matrice
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 30 31 32 33 34 35 36 37
| module mod_matrix
implicit none
public :: Matrix
type Matrix
integer, private :: nrow
integer, private :: ncolumn
real(kind = 8), dimension(:,:), allocatable, private :: array
contains
procedure, private :: set_nrow, set_ncolumn, set_array
procedure, public :: get_nrow, get_ncolumn, get_array
end type Matrix
interface Matrix
procedure init
end interface Matrix
contains
type(Matrix) function init(nrow, ncolumn, array)
integer, intent(in) :: nrow, ncolumn
real(kind = 8), dimension(:,:), allocatable, intent(inout) :: array
integer :: ierr
allocate(init%array(nrow, ncolumn), stat = ierr)
call init%set_nrow(nrow)
call init%set_ncolumn(ncolumn)
call init%set_array(array)
deallocate(array, stat = ierr)
return
end function init
! ... Toutes les méthodes ...
end module mod_matrix |
Tous ces attributs sont privés, et donc non accessibles directement depuis d'autres fichiers. J'ai donc créé des méthodes publiques get et set pour pouvoir y accéder, mais aussi un constructeur qui permet de créer un objet Matrix en faisant:
Code:
1 2 3 4 5 6 7
| integer :: nr, nc
real, dimension(:,:), allocatable :: A
type(Matrix) :: matA
! ... Code pour créer A ...
matA = Matrix(nr, nc, A) |
Dans un code de test (juste un main.f90 et le module mod_matrix.f90), ça marche sans problème.
Maintenant, lorsque je l'insère dans mon "vrai" programme, j'obtiens cette erreur lorsque je compile :
Code:
Error: Component 'nrow' at (1) is a PRIVATE component of 'matrix'
Quelqu'un saurait m'expliquer pourquoi?
Merci d'avance à tous ceux qui me répondront.
PS: en attendant, j'ai rendu ces attributs publiques, mais ce n'est pas le but des objets si je ne m'abuse...