Stein
Stein

Reputation: 3277

OOP Error in Fortran

I've got the following Fortran code:

module class_MAT
  implicit none
  private
  public :: load_coo

  type  Mat_CSR
     real, dimension(:), allocatable :: val, colInd, rowPtr
  end type Mat_CSR

contains
  subroutine load_coo(filename, this)
    implicit none

    type(mat_csr) :: this
    character(len=50) , intent(in) :: filename
    character(len=200) :: line
    real, dimension(:), allocatable :: val, colInd, rowPtr
    integer :: i

    open(unit=7, file = filename)
    do i = 1,10
       read(7, '(A)')  line
       write (*,*) line
    end do

    !allocate(v(n,2))

    close(7)
  end subroutine load_coo
end module class_MAT


program main
  use class_MAT
  implicit none

  type(Mat_CSR) :: m
end program main

I basically adapted my program from this example: http://fortranwiki.org/fortran/show/Object-oriented+programming

However I get this error:

gfortran main.f08 -o main -std=f2008 -O2 
main.f08:37.15:

  type(Mat_CSR) :: m
               1
Error: Derived type 'mat_csr' at (1) is being used before it is defined

I start my program with use class_MAT so I don't understand why the compiler doesn't know about Mat_CSR. How do I fix this error? I ran their example and it works fine.

Upvotes: 0

Views: 64

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78316

Your module (in line 3) declares all entities to be private, other than load_coo (line 4). Then you bash ahead and try to use one of those private entities in your program.

Upvotes: 2

Related Questions