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













0















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









share|improve this question
























  • 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 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











  • Dummy arguments of type class should be intent(inout).

    – laserbrain
    yesterday















0















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









share|improve this question
























  • 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 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











  • Dummy arguments of type class should be intent(inout).

    – laserbrain
    yesterday













0












0








0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • Dummy arguments of type class should be intent(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











  • 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











  • 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
















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












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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현