Original article by Mark Leair, PGI Compiler Engineer
Note: This article was revised in March 2015 and again in January 2016 to bring it up-to-date with the production software release and to correct errors in the examples.
Replace 'class(* ), value' with ' class(* ) :: value'
(Same error is also in list.f90 at line 50)
Correct code snippet:
subroutine addValue(this, value)
class(list) :: this
class(*) :: value
class(link), pointer :: newLink
There are also (other) errors in integer_list.f90 what makes that that module doesn't compile with Intel Parallel Studio XE 2017
but it does with pgfortran.
Hi,
When you try to create a class object, fortran gives an error. This happens if the object is created not immediately after the declaration of data types, but in the text of the program. The example is taken http://fortranwiki.org/fortran/show/Object-oriented%20programming
At the end of the code, I added the lines:
type (Circle) :: c1
c1 = Circle (1.7)
When you try to create an object "c1", the compiler issues an error (including if you remove implicit none) Error: Unexpected data declaration statement statement (1).
How, using OOP fortran, to create an object in the process of executing the program?
I'm using the Gfortran 7.3 compiler in ubuntu
Thank you.
The text of the program with an attempt to create the object "C1"
module class_Circle
implicit none
private
public :: Circle, circle_area, circle_print
real :: pi = 3.1416 ! Class-wide private constant
type Circle
real :: radius
end type Circle
contains
function circle_area(this) result(area)
type(Circle), intent(in) :: this
real :: area
area = pi * this%radius**2
end function circle_area
subroutine circle_print(this)
type(Circle), intent(in) :: this
real :: area
area = circle_area(this) ! Call the circle_area function
print *, 'Circle: r = ', this%radius, ' area = ', area
end subroutine circle_print
end module class_Circle
program circle_test
use class_Circle
implicit none
type(Circle) :: c ! Declare a variable of type Circle.
c = Circle(1.5) ! Use the implicit constructor, radius = 1.5.
call circle_print(c) ! Call a class subroutine
!New object
type(Circle) :: c1 ! Error: Unexpected data declaration statement at (1)
c1 = Circle(1.7)
end program circle_test
Hi,
When you try to create a class object, fortran gives an error. This happens if the object is created not immediately after the declaration of data types, but in the text of the program. The example is taken http://fortranwiki.org/fortran/show/Object-oriented%20programmingAt the end of the code, I added the lines:
type (Circle) :: c1 c1 = Circle (1.7)When you try to create an object "c1", the compiler issues an error (including if you remove implicit none) Error: Unexpected data declaration statement statement (1).
How, using OOP fortran, to create an object in the process of executing the program?
I'm using the Gfortran 7.3 compiler in ubuntu
Thank you.The text of the program with an attempt to create the object "C1"
module class_Circle implicit none private public :: Circle, circle_area, circle_print real :: pi = 3.1416 ! Class-wide private constant type Circle real :: radius end type Circle contains function circle_area(this) result(area) type(Circle), intent(in) :: this real :: area area = pi * this%radius**2 end function circle_area subroutine circle_print(this) type(Circle), intent(in) :: this real :: area area = circle_area(this) ! Call the circle_area function print *, 'Circle: r = ', this%radius, ' area = ', area end subroutine circle_print end module class_Circle program circle_test use class_Circle implicit none type(Circle) :: c ! Declare a variable of type Circle. c = Circle(1.5) ! Use the implicit constructor, radius = 1.5. call circle_print(c) ! Call a class subroutine !New object type(Circle) :: c1 ! Error: Unexpected data declaration statement at (1) c1 = Circle(1.7) end program circle_test
Hi,
in Fortran data declaration happens at the beginning of an entity (program, module, subroutine, function) in contrast to other programming languages. It isn't allowed afterwards.
Replace 'class(* ), value' with ' class(* ) :: value'
(Same error is also in list.f90 at line 50)
Correct code snippet:subroutine addValue(this, value)
class(list) :: this
class(*) :: value
class(link), pointer :: newLinkThere are also (other) errors in integer_list.f90 what makes that that module doesn't compile with Intel Parallel Studio XE 2017
but it does with pgfortran.
I finally get to fix that. It should now compile with ifort and gcc.
Tal for the articles.Please consider to add a destroyList procedure. Especially in the second example, where data are copied from the list to an array, list could be destroyed to release memory. Tia
Thank you very much for your example. There is an error in your example code of abstract_list_mod. see
valuedefinitionnomatter what I chose for value Intel Parallel Studio XE 2017 gives me the following error: