INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

errors and debugging in modern Fortran

errors and debugging in modern Fortran

(OP)
Hello,

I have recently begun to learn coding in Fortran for a work project but am stuck on the debugging phase. I have tried to create a class using a module file with a derived type to hold object variables which "contains" several subroutines. Should the subroutines be able to access the variables without my having to declare them in each subroutine all over again?

Some of the common errors I am getting are "invalid delimiter" and notifications that a variable name has already been used or that an array has already been used as a function name. It seems as though the compiler is mistaking an array for a function in some cases, not sure why.

The 'invalid delimiter' issue usually comes up with statements accessing elements of arrays, such as "Psi(1)" or "Theta(I)" where I is an integer loop control variable.
This seems to happen both in my work project and a simpler program I created just to test these things.

Here is the main file of the simple program:

CODE --> Fortran

program Test
use Test_it

    implicit none

    type(resistance) :: thing

    call thing%Display(Psi, Theta)

    end program Test 

The 'invalid delimiter' error highlights "thing&Display(Psi, Theta) as the problem area.

And here is the module file for the above program:

CODE --> FORTRAN

module Test_it

    implicit none

    ! place global type definitions, generic interfaces, 
    ! variable declarations and initializations here

    Type resistance
        real, dimension(10) :: Psi
        real, dimension(10) :: Theta

        end type resistance

contains

subroutine Display (Psi, Theta)
    real, dimension(10) :: Psi, Theta
       
        write (*,*) 'Enter a real number for Psi(1)'
        read (*,*) Psi(1)

        write (*,*) 'Enter a real number for Theta(1)'
        read (*,*) Theta(1)

        write (*,*) 'Psi(1):', Psi(1)
        write (*,*) 'Theta(1):', Theta(1)
       
end subroutine Display

end module Test_it 

I would greatly appreciate anyone's insight into this matter and advice on how to best debug a lengthy, complex Fortran program.

Thank you,

Kei

RE: errors and debugging in modern Fortran

Hint 1: Please re-read how to use module; I am afraid you are misunderstanding how to use them.

Hint 2: everything in the module is available to you at a single level as if type resistance and subroutine display were siblings. So, no need to do thing%display, in fact it is wrong. All you need to do is "call display(Psi, Theta)".

Hint 3: yes, variables declared in the module are available inside the contained subroutines.

Try this module:

CODE

module Test_it

    implicit none
    real, dimension(10) :: Psi
    real, dimension(10) :: Theta

contains

    subroutine ReadValues ()   
        write (*,*) 'Enter a real number for Psi(1)'
        read (*,*) Psi(1)
        write (*,*) 'Enter a real number for Theta(1)'
        read (*,*) Theta(1)
    end subroutine ReadValues

    subroutine Display ()
        write (*,*) 'Psi(1):', Psi(1)
        write (*,*) 'Theta(1):', Theta(1)      
    end subroutine Display

end module Test_it 
with this main program:

CODE

program Test
use Test_it
    implicit none
    call ReadValues()
    call Display()
end program Test 

RE: errors and debugging in modern Fortran

(OP)
hello and thanks for your response.

I did try typing in what you suggested but I still get various errors. The IDE is Lahey Fortran 95 using Microsoft Visual 2012 as the shell. We tried compiling code featuring a class from Lahey's website and even that didn't work, so now we think there may be something wrong with the IDE software itself.

Anyway, isn't it usually necessary to declare an object of the class type in the main file? That's why I use the % accessor. Some of the references I have been looking at make use of it as well.

RE: errors and debugging in modern Fortran

I don't usually program using classes or with anything beyond Fortran 95. By the way, what you seem to be calling class is actually called "derived data type" in Fortran; I think is kind of equivalent to structures from C...not necessarily what understand a class to be.

In any case, I use gfortran to compile the code provided.

RE: errors and debugging in modern Fortran

I have no much experience with this OOP stuff in Fortran, but IMO If you want call the procedure like this

CODE

type(resistance) :: thing
...
call thing%Display(Psi, Theta) 
you have to define procedure pointer in your structure resistance.

For example see this thread:
http://www.tek-tips.com/viewthread.cfm?qid=1594819

But note that this approach doesn't work in all current Fortran compilers.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close