Fortran程序报错:'side1'等symbol无隐式类型,求解决方案
问题原因
你遇到的“symbol 'side2' 无隐式类型”等错误,核心原因是主程序中启用了implicit none,但未显式声明side1、side2、hypo这三个变量的类型。
implicit none会关闭Fortran默认的隐式类型推断规则(默认以I-N开头的变量为整数,其余为实数),要求所有用到的变量必须提前声明类型,否则编译器无法识别变量类型,就会抛出这类错误。
解决方案
在主程序的implicit none语句之后,添加这三个变量的类型声明即可:
real :: side1, side2, hypo
修改后的完整代码
PROGRAM main !to test subroutines use share_data implicit none real :: side1, side2, hypo ! 新增变量声明 write (*,1) 1 format (T10, 'Enter the lengths of 2 edges of a right triangle to count the hypotenus:', /) read (*,*) side1, side2 call hypotenuse(side1,side2,hypo) write (*,2) side1, side2, hypo 2 format (2X, 'side1=', F10.2, /, 2X, 'side2=', F10.2, /, 2X, 'hypotenuse=', F10.2, /) END PROGRAM main MODULE share_data implicit none save real :: x, avg, std integer, parameter :: N = 8 real :: arr(N), vec(N) character(20) :: str integer :: coun, error CONTAINS SUBROUTINE hypotenuse(side1,side2,hypo) !A subroutine to calculate hypotenuse of a right triangle from two other sides implicit none real, intent(in) :: side1, side2 real, intent(out) :: hypo !calculate hypo hypo = sqrt(side1 ** 2 + side2 ** 2) END SUBROUTINE hypotenuse END MODULE share_data
内容的提问来源于stack exchange,提问作者Morteza
相关产品推荐
相关产品推荐

