Fortran 中的 OOP 错误

OOP Error in Fortran

我有以下 Fort运行 代码:

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

我基本上是根据这个例子改编我的程序: http://fortranwiki.org/fortran/show/Object-oriented+programming

但是我得到这个错误:

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

我的程序以 use class_MAT 开始,所以我不明白为什么编译器不知道 Mat_CSR。我该如何解决这个错误?我 运行 他们的例子,它工作正常。

您的模块(第 3 行)将所有实体声明为 privateload_coo(第 4 行)除外。然后你 bash 提前尝试在你的程序中使用这些私有实体之一。