我有一个关于在 Fortran 90 中的模块内使用子例程的问题。这是我的代码
Module Multiplication
Subroutine Two_times(input,output)
Real :: input,output
output = input * 2.0
End Subroutine Two_times
End Module
Program test_get_command_argument
Use Multiplication: Two_times
Real :: i,j
i = 0.5
Write (*,*) i
Call Two_times(i,j)
Write (*,*) j
End Program
我使用ifort编译了上面的代码。我收到以下消息。
files_rev.f90(2): error #6218: This statement is positioned incorrectly and/or has syntax errors.
Subroutine Two_times(input,output)
--^
files_rev.f90(4): error #6274: This statement must not appear in the specification part of a module.
output = input * 2.0
----^
files_rev.f90(5): error #6786: This is an invalid statement; an END [MODULE] statement is required.
End Subroutine Two_times
--^
files_rev.f90(5): error #6785: This name does not match the unit name. [TWO_TIMES]
End Subroutine Two_times
-----------------^
files_rev.f90(6): error #6790: This is an invalid statement; an END [PROGRAM] statement is required.
End Module
^
files_rev.f90(9): error #5082: Syntax error, found IDENTIFIER 'MULTIPLICATION' when expecting one of: ( : % [ . = =>
Use Multiplication: Two_times
------^
files_rev.f90(8): warning #5427: Program may contain only one main entry routine
Program test_get_command_argument
--------^
compilation aborted for files_rev.f90 (code 1)
为什么我收到 #6218 和 #6274 错误消息以及如何修复它们?
你缺少一个contains
之前的关键字subroutine
声明和only
之后的关键字use
。或者你可以删除: Two_times
使用模块中的所有内容。所以工作代码如下所示:
Module Multiplication
Contains
Subroutine Two_times(input,output)
Real :: input,output
output = input * 2.0
End Subroutine Two_times
End Module
Program test_get_command_argument
Use Multiplication, Only: Two_times
Real :: i,j
i = 0.5
Write (*,*) i
Call Two_times(i,j)
Write (*,*) j
End Program
看看这个答案,无法在同一文件中使用模块和主程序进行编译,同样,对于失踪者contains
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)