"PROCEDURE属性与INTENT属性冲突".使用模块编译简单的Fortran程序时 [英] "PROCEDURE attribute conflicts with INTENT attribute" when compiling simple Fortran program with module

查看:70
本文介绍了"PROCEDURE属性与INTENT属性冲突".使用模块编译简单的Fortran程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Fortran 95程序

I have a simple Fortran 95 program

include "sandboxlib.f95"
program sandbox
    implicit none
    write(*, *) 'abc'
end program

和一个包含函数的简单模块

and a simple module containing a function

module sandboxlib

 integer, parameter :: dp = kind(1.d0)

contains
function cumsum(mat, m, n) result(c)
    implicit none
    real(dp), intent(in) :: mat
    integer, intent(in) :: m, n
    integer i, j
    real(dp), dimension(m, n) :: c

    c(:, 1) = 0.d0

    do i = 2, m
        do j = 1, n
            c(i, j) = c(i-1, j) + mat(i, j)
        end do
    end do
end function
end module

我使用此命令编译 sandbox.f95

/usr/bin/gfortran -O -std=gnu -Wfatal-errors -pedantic -Wall sandbox.f95 -o sandbox

这将导致此错误

sandboxlib.f95:6.23:
    Included at sandbox.f95:1:

    function cumsum(mat, m, n)
                       1
Error: PROCEDURE attribute conflicts with INTENT attribute in 'mat' at (1)

我环顾四周,发现很少

I looked around and found a few questions that discuss modules, functions, etc. or an error like this, but I can't figure out why this won't compile.

推荐答案

您的 mat 被声明为标量

  real(dp), intent(in) :: mat

但是您将其用作数组

  c(i, j) = c(i-1, j) + mat(i, j)

,编译器将此解析为函数调用,并假定 mat()是一个函数.并且函数不能具有 intent .

and the compiler parses this as a function call and assumes mat() is a function. And functions cannot have intent.

我认为正确的做法是在声明中将 mat 设置为数组.像

I assume the correct thing to do is to make mat an array in the declaration. Something like

  real(dp), intent(in) :: mat(:,:)

  real(dp), intent(in) :: mat(m,n) 

使用前者,您可以避免将 m n 作为参数传递.

With the former you can avoid passing m and n as arguments.

这篇关于"PROCEDURE属性与INTENT属性冲突".使用模块编译简单的Fortran程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆