Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29273 Discussions

Access violation: Trying to use Class(*)

abhimodak
New Contributor I
2,145 Views
The snippet below compiles fine but gives an access violation error at line # 45. The debugger does show the "Object" components correctly.

Abhi

====

Module myTest

Implicit None

Contains

Subroutine Test(Object, i, Method)

Implicit None

Class(*) :: Object

Integer :: i

External :: Method

Call Method(Object, i)

End Subroutine Test

End Module myTest

Module myType

Implicit None

Type newObject
Integer :: i = 1
Real :: r = 0.0
Contains
Procedure :: ShowMe
End Type newObject

Contains

Subroutine ShowMe(Object, i)

Implicit None

Class(newObject) :: Object

Integer :: i

Print *, i
Print *, Object%i

End Subroutine ShowMe

End Module myType

Program Test_Class

Use myType
Use myTest

Implicit None

Type(newObject) :: Object

Call Test(Object, Object%i, ShowMe)

End Program Test_Class

0 Kudos
1 Solution
reinhold-bader
New Contributor II
2,145 Views
Some remarks about Steve's version of the code:

Error 1 can in this case very simply be avoided by not making showme a type-bound procedure of newobject (one is not obliged to do so, and there are some (other) situations where it is in fact not possible).

Error 2 however I consider to be spurious, since a CLASS(*) dummy should match an actual of any type. To confirm this I also tried NAG's compiler, which had no difficulties with the "no-TBP" version of the code.

Regards
Reinhold

View solution in original post

0 Kudos
12 Replies
Steven_L_Intel1
Employee
2,145 Views

"Method" needs an explicit interface because the called routine has a polymorphic argument. EXTERNAL is not sufficient. I will admit that I don't yet have my head wrapped around all this OO stuff, and I'm having difficulty modifying your code to be valid. I've gotten this far:

[plain]Module myTest

Implicit None

Contains

Subroutine Test(Object, i, Method)

Implicit None

Class(*) :: Object

Integer :: i

interface
subroutine Method (Object, i)
import
class(*) :: Object
integer :: i
end subroutine Method
end interface

Call Method(Object, i)

End Subroutine Test

End Module myTest

Module myType

Implicit None

Type newObject
Integer :: i = 1
Real :: r = 0.0
Contains
Procedure :: ShowMe
End Type newObject

Contains

Subroutine ShowMe(Object, i)

Implicit None

Class(*) :: Object !???

Integer :: i

select type (Object)
type is (newObject)
Print *, i
Print *, Object%i
end select

End Subroutine ShowMe

End Module myType

Program Test_Class

Use myType
Use myTest

Implicit None

Type(newObject) :: Object

Call Test(Object, Object%i, ShowMe)

End Program Test_Class[/plain]
But the compiler complains:

(42) error #8262: The passed-object dummy argument must be dummy data object with the same declared type as the type being defined. [OBJECT]
(69) error #7061: The characteristics of dummy argument 1 of the associated actual procedure differ from the characteristics of dummy argument 1 of the dummy procedure. (12.2) [SHOWME]



0 Kudos
abhimodak
New Contributor I
2,145 Views
Hi Steve

As I understand the error 1 is because the rule on the passed-object for the type-bound procedure and error 2 is because class(*) is not equal to another class(*). {However, I am unsure if the explicit interface is "required". If so, it should have given a compile-time error.}

What I am trying to do is to get Subroutine Test call Method(Object, i, r, ...). Subroutine Test itself has nothing to do with the "Object". The arguments i, r etc will be common to all object types.

In the snippet below I have used abstract type and thus can achieve what I was trying to do. Clearly, I cannot "free" the subroutine Test of knowing the abstract type (i.e. use Class(*)).


Abhi


====


Module Basic

Implicit None

Type, Abstract :: Parent
Contains
Procedure(Scheme), Deferred :: Method
End Type

Abstract Interface
Subroutine Scheme(Model, i)
Import
Class(Parent) :: Model
Integer :: i
End Subroutine Scheme
End Interface

End Module Basic

Module Derived

Use Basic

Type, Extends(Parent) :: Child
Integer :: m
Real :: r
Contains
Procedure :: Method
End Type

Contains

Subroutine Method(Model, i)
Implicit None
Class(Child) :: Model
Integer :: i
Print *, i
Print *, Model%m
End Subroutine Method

End Module Derived

Module myTest

Use Basic

Implicit None

Contains

Subroutine Test(Model, i)

Implicit None

Class(Parent) :: Model

Integer :: i

Call Model%Method(i)

End Subroutine Test

End Module myTest

Program Test_AbstractType

Use myTest
Use Derived

Implicit None

Type(Child) :: This

Integer :: n = 3

This%m = 1
This%r = 0.0

Call Test(This, n)

End Program Test_AbstractType


0 Kudos
Steven_L_Intel1
Employee
2,145 Views
I agree that the compiler should not let you pass a class(*) object to a routine with an implicit interface. I'll pass that on.
0 Kudos
reinhold-bader
New Contributor II
2,146 Views
Some remarks about Steve's version of the code:

Error 1 can in this case very simply be avoided by not making showme a type-bound procedure of newobject (one is not obliged to do so, and there are some (other) situations where it is in fact not possible).

Error 2 however I consider to be spurious, since a CLASS(*) dummy should match an actual of any type. To confirm this I also tried NAG's compiler, which had no difficulties with the "no-TBP" version of the code.

Regards
Reinhold
0 Kudos
Steven_L_Intel1
Employee
2,145 Views
I agree - entered as bug DPD200139700.
0 Kudos
Steven_L_Intel1
Employee
2,145 Views
The standard says that even though unlimited polymorphic CLASS(*) has "no declared type", it is type compatible with everything.
0 Kudos
Steven_L_Intel1
Employee
2,145 Views
I've reported A. Do you have a test case for B? I don't see an "UNPOLY" anywhere.
0 Kudos
abhimodak
New Contributor I
2,145 Views
Hi Steve

I get the error in with the snippet I posted. I am using IVF11.1.038 on WinXP64 with VS2005 professional. The "output" window has the following:-

1>------ Rebuild All started: Project: Test_Class, Configuration: Debug Win32 ------
1>Deleting intermediate files and output files for project 'Test_Class', configuration 'Debug|Win32'.
1>Compiling with Intel Visual Fortran 11.1.038 [IA-32]...
1>Test_Class.f90
1>C:AbhiMySourceTestsTest_ClassTest_Class.f90(69): error #6404: This name does not have a type, and must have an explicit type. [#UNLPOLY]
1>(72): catastrophic error: Internal Compiler Error: possible out of order or missing USE
1>compilation aborted for C:AbhiMySourceTestsTest_ClassTest_Class.f90 (code 1)
1>
1>Build log written to "file://C:AbhiMySourceTestsTest_ClassDebugBuildLog.htm"
1>Test_Class - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
0 Kudos
Steven_L_Intel1
Employee
2,145 Views
Oh, I get it now. Yes, I'll report that. Issue ID is DPD200139745.
0 Kudos
Steven_L_Intel1
Employee
2,145 Views
This problem is corrected in 11.1 Update 3, available now.
0 Kudos
Reply