Allocating array of abstract type in FortranWhat's the canonical way to check for type in Python?What is the difference between an abstract function and a virtual function?How to determine a Python variable's type?Interface vs Abstract Class (general OO)What are the differences between type() and isinstance()?What is the difference between an interface and abstract class?allocatable user derived typesProcedure copy in each instance of data typeFortran syntax for assignmentsArray of abstract type subroutines - Fortran
Is the U.S. Code copyrighted by the Government?
Why Shazam when there is already Superman?
It grows, but water kills it
Drawing ramified coverings with tikz
How should I respond when I lied about my education and the company finds out through background check?
Offered money to buy a house, seller is asking for more to cover gap between their listing and mortgage owed
Is it possible to have a strip of cold climate in the middle of a planet?
Why electric field inside a cavity of a non-conducting sphere not zero?
How do I color the graph in datavisualization?
When were female captains banned from Starfleet?
Travelling outside the UK without a passport
How to indicate a cut out for a product window
Open a doc from terminal, but not by its name
If infinitesimal transformations commute why dont the generators of the Lorentz group commute?
What are the purposes of autoencoders?
How to explain what's wrong with this application of the chain rule?
Yosemite Fire Rings - What to Expect?
"Spoil" vs "Ruin"
Why did the EU agree to delay the Brexit deadline?
How to bake one texture for one mesh with multiple textures blender 2.8
Is there any references on the tensor product of presentable (1-)categories?
250 Floor Tower
Why can Carol Danvers change her suit colours in the first place?
Removing files under particular conditions (number of files, file age)
Allocating array of abstract type in Fortran
What's the canonical way to check for type in Python?What is the difference between an abstract function and a virtual function?How to determine a Python variable's type?Interface vs Abstract Class (general OO)What are the differences between type() and isinstance()?What is the difference between an interface and abstract class?allocatable user derived typesProcedure copy in each instance of data typeFortran syntax for assignmentsArray of abstract type subroutines - Fortran
Basically, I have an abstract class a wrapper class and a base class which are defined in the Base Module. The abbstract class holds an allocatable array and a subroutine. In the constructor I want to allocate this array but that does not work.
The allocation does work for Base
but I guess that is not what I want as the Child Class does not know anything from Base
. How would I do this allocation?
Here's what I have:
Base Module
module BaseClass
implicit none
! ------------------------
! ABSTRACT CLASS
! ------------------------
type, abstract :: AbsBaseHelper
real, allocatable :: A(:) !! <-- this makes problems
contains
procedure :: construct
procedure(help_int), deferred :: help
end type
! ------------------------
! WRAPPER CLASS
! ------------------------
type :: BaseWrap
integer :: cI
class(AbsBaseHelper), pointer :: p
contains
procedure :: do_something
end type
! ------------------------
! INTERFACE
! ------------------------
interface
subroutine help_int(this)
import AbsBaseHelper
implicit none
class(AbsBaseHelper), intent(inout) :: this
end subroutine help_int
end interface
! ------------------------
! BASE CLASS
! ------------------------
type(BaseWrap) :: Base(2)
contains
! ------------------------
! CONSTRUCTOR
! ------------------------
subroutine construct(this, id)
implicit none
class(AbsBaseHelper), intent(in), target :: this
integer, intent(in) :: id
Base(id)%cI = id
! allocate( this%A(2) ) !! <-- does not work because this is only intent(in)
Base(id)%p => this
allocate( Base(id)%p%A(2) ) !! <-- does not work because it gives segmentation fault in 'help'
end subroutine construct
! ------------------------
! THE MAIN SUBROUTINE
! ------------------------
subroutine do_something(this)
implicit none
class(BaseWrap), intent(inout) :: this
print*, "Base Index : ", this%cI
call this%p%help()
print*, "Result 1 : ", this%p%A(1)
print*, "Result 2 : ", this%p%A(2)
end subroutine do_something
end module BaseClass
Child Module
module ChildClass1
use BaseClass
implicit none
type, extends(AbsBaseHelper) :: Child1
contains
procedure :: help
end type
contains
subroutine help(this)
implicit none
class(Child1), intent(inout) :: this
this%A(1) = 1 !! <-- produces segmentation fault
this%A(2) = 2
end subroutine
end module ChildClass1
The Program
program test
use BaseClass
implicit none
call init
call Base(1)%do_something()
contains
! ------------------------
! INITIALIZE
! ------------------------
subroutine init
use ChildClass1
implicit none
type(Child1), target :: c1
call c1%construct(1)
end subroutine init
end program test
oop types fortran subroutine
add a comment |
Basically, I have an abstract class a wrapper class and a base class which are defined in the Base Module. The abbstract class holds an allocatable array and a subroutine. In the constructor I want to allocate this array but that does not work.
The allocation does work for Base
but I guess that is not what I want as the Child Class does not know anything from Base
. How would I do this allocation?
Here's what I have:
Base Module
module BaseClass
implicit none
! ------------------------
! ABSTRACT CLASS
! ------------------------
type, abstract :: AbsBaseHelper
real, allocatable :: A(:) !! <-- this makes problems
contains
procedure :: construct
procedure(help_int), deferred :: help
end type
! ------------------------
! WRAPPER CLASS
! ------------------------
type :: BaseWrap
integer :: cI
class(AbsBaseHelper), pointer :: p
contains
procedure :: do_something
end type
! ------------------------
! INTERFACE
! ------------------------
interface
subroutine help_int(this)
import AbsBaseHelper
implicit none
class(AbsBaseHelper), intent(inout) :: this
end subroutine help_int
end interface
! ------------------------
! BASE CLASS
! ------------------------
type(BaseWrap) :: Base(2)
contains
! ------------------------
! CONSTRUCTOR
! ------------------------
subroutine construct(this, id)
implicit none
class(AbsBaseHelper), intent(in), target :: this
integer, intent(in) :: id
Base(id)%cI = id
! allocate( this%A(2) ) !! <-- does not work because this is only intent(in)
Base(id)%p => this
allocate( Base(id)%p%A(2) ) !! <-- does not work because it gives segmentation fault in 'help'
end subroutine construct
! ------------------------
! THE MAIN SUBROUTINE
! ------------------------
subroutine do_something(this)
implicit none
class(BaseWrap), intent(inout) :: this
print*, "Base Index : ", this%cI
call this%p%help()
print*, "Result 1 : ", this%p%A(1)
print*, "Result 2 : ", this%p%A(2)
end subroutine do_something
end module BaseClass
Child Module
module ChildClass1
use BaseClass
implicit none
type, extends(AbsBaseHelper) :: Child1
contains
procedure :: help
end type
contains
subroutine help(this)
implicit none
class(Child1), intent(inout) :: this
this%A(1) = 1 !! <-- produces segmentation fault
this%A(2) = 2
end subroutine
end module ChildClass1
The Program
program test
use BaseClass
implicit none
call init
call Base(1)%do_something()
contains
! ------------------------
! INITIALIZE
! ------------------------
subroutine init
use ChildClass1
implicit none
type(Child1), target :: c1
call c1%construct(1)
end subroutine init
end program test
oop types fortran subroutine
Could you please try to reduce your problem to a Minimal, Complete, and Verifiable example (shorter)? There is a lot going on there, this may be uninvitative for users to put attention in your question.
– Rodrigo Rodrigues
2 days ago
Hi @RodrigoRodrigues! I now completely reformulated the question and trid to minify it to the maximum possible. I hope this still makes sense.
– Florian Ragossnig
2 days ago
The errors in the code seem to have little to do with the question title. Consider the lifetime of the local variablec1
in the internal procedureinit
of the main program, and what happens to pointers that are associated with that local variable. Consider also theintent(in)
attribute being applied to a dummy argument that apparently needs to be defined by the relevant procedure. In terms of the code in the question, consider describing what you are trying to do, as well as how you are trying to do it.
– IanH
yesterday
Thanks for the comment @IanH. Unfortunately this is the point; I don't know how to solve this. Any suggestions what I could try?
– Florian Ragossnig
yesterday
Dummy arguments of typeclass
should beintent(inout)
.
– laserbrain
yesterday
add a comment |
Basically, I have an abstract class a wrapper class and a base class which are defined in the Base Module. The abbstract class holds an allocatable array and a subroutine. In the constructor I want to allocate this array but that does not work.
The allocation does work for Base
but I guess that is not what I want as the Child Class does not know anything from Base
. How would I do this allocation?
Here's what I have:
Base Module
module BaseClass
implicit none
! ------------------------
! ABSTRACT CLASS
! ------------------------
type, abstract :: AbsBaseHelper
real, allocatable :: A(:) !! <-- this makes problems
contains
procedure :: construct
procedure(help_int), deferred :: help
end type
! ------------------------
! WRAPPER CLASS
! ------------------------
type :: BaseWrap
integer :: cI
class(AbsBaseHelper), pointer :: p
contains
procedure :: do_something
end type
! ------------------------
! INTERFACE
! ------------------------
interface
subroutine help_int(this)
import AbsBaseHelper
implicit none
class(AbsBaseHelper), intent(inout) :: this
end subroutine help_int
end interface
! ------------------------
! BASE CLASS
! ------------------------
type(BaseWrap) :: Base(2)
contains
! ------------------------
! CONSTRUCTOR
! ------------------------
subroutine construct(this, id)
implicit none
class(AbsBaseHelper), intent(in), target :: this
integer, intent(in) :: id
Base(id)%cI = id
! allocate( this%A(2) ) !! <-- does not work because this is only intent(in)
Base(id)%p => this
allocate( Base(id)%p%A(2) ) !! <-- does not work because it gives segmentation fault in 'help'
end subroutine construct
! ------------------------
! THE MAIN SUBROUTINE
! ------------------------
subroutine do_something(this)
implicit none
class(BaseWrap), intent(inout) :: this
print*, "Base Index : ", this%cI
call this%p%help()
print*, "Result 1 : ", this%p%A(1)
print*, "Result 2 : ", this%p%A(2)
end subroutine do_something
end module BaseClass
Child Module
module ChildClass1
use BaseClass
implicit none
type, extends(AbsBaseHelper) :: Child1
contains
procedure :: help
end type
contains
subroutine help(this)
implicit none
class(Child1), intent(inout) :: this
this%A(1) = 1 !! <-- produces segmentation fault
this%A(2) = 2
end subroutine
end module ChildClass1
The Program
program test
use BaseClass
implicit none
call init
call Base(1)%do_something()
contains
! ------------------------
! INITIALIZE
! ------------------------
subroutine init
use ChildClass1
implicit none
type(Child1), target :: c1
call c1%construct(1)
end subroutine init
end program test
oop types fortran subroutine
Basically, I have an abstract class a wrapper class and a base class which are defined in the Base Module. The abbstract class holds an allocatable array and a subroutine. In the constructor I want to allocate this array but that does not work.
The allocation does work for Base
but I guess that is not what I want as the Child Class does not know anything from Base
. How would I do this allocation?
Here's what I have:
Base Module
module BaseClass
implicit none
! ------------------------
! ABSTRACT CLASS
! ------------------------
type, abstract :: AbsBaseHelper
real, allocatable :: A(:) !! <-- this makes problems
contains
procedure :: construct
procedure(help_int), deferred :: help
end type
! ------------------------
! WRAPPER CLASS
! ------------------------
type :: BaseWrap
integer :: cI
class(AbsBaseHelper), pointer :: p
contains
procedure :: do_something
end type
! ------------------------
! INTERFACE
! ------------------------
interface
subroutine help_int(this)
import AbsBaseHelper
implicit none
class(AbsBaseHelper), intent(inout) :: this
end subroutine help_int
end interface
! ------------------------
! BASE CLASS
! ------------------------
type(BaseWrap) :: Base(2)
contains
! ------------------------
! CONSTRUCTOR
! ------------------------
subroutine construct(this, id)
implicit none
class(AbsBaseHelper), intent(in), target :: this
integer, intent(in) :: id
Base(id)%cI = id
! allocate( this%A(2) ) !! <-- does not work because this is only intent(in)
Base(id)%p => this
allocate( Base(id)%p%A(2) ) !! <-- does not work because it gives segmentation fault in 'help'
end subroutine construct
! ------------------------
! THE MAIN SUBROUTINE
! ------------------------
subroutine do_something(this)
implicit none
class(BaseWrap), intent(inout) :: this
print*, "Base Index : ", this%cI
call this%p%help()
print*, "Result 1 : ", this%p%A(1)
print*, "Result 2 : ", this%p%A(2)
end subroutine do_something
end module BaseClass
Child Module
module ChildClass1
use BaseClass
implicit none
type, extends(AbsBaseHelper) :: Child1
contains
procedure :: help
end type
contains
subroutine help(this)
implicit none
class(Child1), intent(inout) :: this
this%A(1) = 1 !! <-- produces segmentation fault
this%A(2) = 2
end subroutine
end module ChildClass1
The Program
program test
use BaseClass
implicit none
call init
call Base(1)%do_something()
contains
! ------------------------
! INITIALIZE
! ------------------------
subroutine init
use ChildClass1
implicit none
type(Child1), target :: c1
call c1%construct(1)
end subroutine init
end program test
oop types fortran subroutine
oop types fortran subroutine
edited 2 days ago
Florian Ragossnig
asked 2 days ago
Florian RagossnigFlorian Ragossnig
320314
320314
Could you please try to reduce your problem to a Minimal, Complete, and Verifiable example (shorter)? There is a lot going on there, this may be uninvitative for users to put attention in your question.
– Rodrigo Rodrigues
2 days ago
Hi @RodrigoRodrigues! I now completely reformulated the question and trid to minify it to the maximum possible. I hope this still makes sense.
– Florian Ragossnig
2 days ago
The errors in the code seem to have little to do with the question title. Consider the lifetime of the local variablec1
in the internal procedureinit
of the main program, and what happens to pointers that are associated with that local variable. Consider also theintent(in)
attribute being applied to a dummy argument that apparently needs to be defined by the relevant procedure. In terms of the code in the question, consider describing what you are trying to do, as well as how you are trying to do it.
– IanH
yesterday
Thanks for the comment @IanH. Unfortunately this is the point; I don't know how to solve this. Any suggestions what I could try?
– Florian Ragossnig
yesterday
Dummy arguments of typeclass
should beintent(inout)
.
– laserbrain
yesterday
add a comment |
Could you please try to reduce your problem to a Minimal, Complete, and Verifiable example (shorter)? There is a lot going on there, this may be uninvitative for users to put attention in your question.
– Rodrigo Rodrigues
2 days ago
Hi @RodrigoRodrigues! I now completely reformulated the question and trid to minify it to the maximum possible. I hope this still makes sense.
– Florian Ragossnig
2 days ago
The errors in the code seem to have little to do with the question title. Consider the lifetime of the local variablec1
in the internal procedureinit
of the main program, and what happens to pointers that are associated with that local variable. Consider also theintent(in)
attribute being applied to a dummy argument that apparently needs to be defined by the relevant procedure. In terms of the code in the question, consider describing what you are trying to do, as well as how you are trying to do it.
– IanH
yesterday
Thanks for the comment @IanH. Unfortunately this is the point; I don't know how to solve this. Any suggestions what I could try?
– Florian Ragossnig
yesterday
Dummy arguments of typeclass
should beintent(inout)
.
– laserbrain
yesterday
Could you please try to reduce your problem to a Minimal, Complete, and Verifiable example (shorter)? There is a lot going on there, this may be uninvitative for users to put attention in your question.
– Rodrigo Rodrigues
2 days ago
Could you please try to reduce your problem to a Minimal, Complete, and Verifiable example (shorter)? There is a lot going on there, this may be uninvitative for users to put attention in your question.
– Rodrigo Rodrigues
2 days ago
Hi @RodrigoRodrigues! I now completely reformulated the question and trid to minify it to the maximum possible. I hope this still makes sense.
– Florian Ragossnig
2 days ago
Hi @RodrigoRodrigues! I now completely reformulated the question and trid to minify it to the maximum possible. I hope this still makes sense.
– Florian Ragossnig
2 days ago
The errors in the code seem to have little to do with the question title. Consider the lifetime of the local variable
c1
in the internal procedure init
of the main program, and what happens to pointers that are associated with that local variable. Consider also the intent(in)
attribute being applied to a dummy argument that apparently needs to be defined by the relevant procedure. In terms of the code in the question, consider describing what you are trying to do, as well as how you are trying to do it.– IanH
yesterday
The errors in the code seem to have little to do with the question title. Consider the lifetime of the local variable
c1
in the internal procedure init
of the main program, and what happens to pointers that are associated with that local variable. Consider also the intent(in)
attribute being applied to a dummy argument that apparently needs to be defined by the relevant procedure. In terms of the code in the question, consider describing what you are trying to do, as well as how you are trying to do it.– IanH
yesterday
Thanks for the comment @IanH. Unfortunately this is the point; I don't know how to solve this. Any suggestions what I could try?
– Florian Ragossnig
yesterday
Thanks for the comment @IanH. Unfortunately this is the point; I don't know how to solve this. Any suggestions what I could try?
– Florian Ragossnig
yesterday
Dummy arguments of type
class
should be intent(inout)
.– laserbrain
yesterday
Dummy arguments of type
class
should be intent(inout)
.– laserbrain
yesterday
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281450%2fallocating-array-of-abstract-type-in-fortran%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281450%2fallocating-array-of-abstract-type-in-fortran%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you please try to reduce your problem to a Minimal, Complete, and Verifiable example (shorter)? There is a lot going on there, this may be uninvitative for users to put attention in your question.
– Rodrigo Rodrigues
2 days ago
Hi @RodrigoRodrigues! I now completely reformulated the question and trid to minify it to the maximum possible. I hope this still makes sense.
– Florian Ragossnig
2 days ago
The errors in the code seem to have little to do with the question title. Consider the lifetime of the local variable
c1
in the internal procedureinit
of the main program, and what happens to pointers that are associated with that local variable. Consider also theintent(in)
attribute being applied to a dummy argument that apparently needs to be defined by the relevant procedure. In terms of the code in the question, consider describing what you are trying to do, as well as how you are trying to do it.– IanH
yesterday
Thanks for the comment @IanH. Unfortunately this is the point; I don't know how to solve this. Any suggestions what I could try?
– Florian Ragossnig
yesterday
Dummy arguments of type
class
should beintent(inout)
.– laserbrain
yesterday