Let's say I have a variable foo
which is of class(BaseType)
. Type1
and Type2
are types that extends BaseType
, and I am trying to allocate foo
to be the same type as a second variable, bar
, which has previously been declared as either Type1
or Type2
.
When I try to write a subroutine using the function TYPE
to do this, I get a compilation error:
SUBROUTINE TESTING(foo,bar)
CLASS(BaseType), INTENT(INOUT) :: foo
CLASS(BaseType), INTENT(IN) :: bar
ALLOCATE(TYPE(bar)::foo)
END SUBROUTINE TESTING
The compilation error is:
Error: Allocate-object at (1) is neither a data pointer nor an allocatable variable
Is there a way to do what I am describing? Or is there an error with my implementation? This subroutine is written in a module if that is relevant. Thanks in advance.
The specifier mold=
is exactly for this but you also have to fix what the error message is complaining about. foo
is neither allocatable
nor pointer
and it must be one of these to appear in allocate
. After fixing that, you can do
allocate(bar, mold=foo)