调用返回数组的函数时出现Fortran Error 256问题求助
Hey there, let's tackle that Error 256 you're hitting when calling swap, scale, and pivot in your Fortran program. In FTN95, this error almost always boils down to argument mismatches between your subroutine calls and the subroutine definitions—let's walk through the most likely fixes, since you need these functions to modify your input matrix.
Common Causes & Fixes
1. Incorrect Argument Intent
If your subroutines are supposed to modify the input matrix, their array parameters must be declared with intent(inout) (explicit intent is better practice than omitting it). If you accidentally used intent(in), FTN95 will throw Error 256 because you're trying to write to a read-only argument.
Correct Example for a Swap Subroutine:
subroutine swap(mat, row1, row2) implicit none real, intent(inout) :: mat(:,:) ! Allows reading and modifying the matrix integer, intent(in) :: row1, row2 real :: temp(size(mat,2)) ! Temp array to hold a full row ! Add bounds checking to avoid invalid indices if (row1 < 1 .or. row1 > size(mat,1) .or. row2 < 1 .or. row2 > size(mat,1)) then print *, "Error: Row indices out of bounds" return end if ! Perform the row swap temp = mat(row1,:) mat(row1,:) = mat(row2,:) mat(row2,:) = temp end subroutine swap
2. Array Dimension Mismatch
Your input is a 3x4 matrix (3 equations, each with 3 coefficients + 1 solution). If your subroutines use fixed-size arrays (e.g., real :: mat(3,4)) but you're passing a dynamic array, or vice versa, FTN95 will flag a mismatch.
Fix: Use assumed-shape arrays (mat(:,:)) in your subroutines. This makes them flexible for any size of 2D matrix and ensures compatibility with your input array, as long as you pass a 2D array when calling the subroutine.
3. Missing or Incorrect Subroutine Interfaces
If your swap, scale, and pivot subroutines are not in a module (or you haven't included an explicit interface block), FTN95 uses implicit interface checking, which can miss subtle mismatches (like integer vs. real arguments, or wrong array rank).
Fix:
- Wrap your subroutines in a module and
usethat module in your main program, or - Add an explicit interface block in your main program for each subroutine. This tells the compiler exactly what arguments to expect.
Example interface block for swap:
interface subroutine swap(mat, row1, row2) real, intent(inout) :: mat(:,:) integer, intent(in) :: row1, row2 end subroutine swap end interface
4. FTN95 Compiler Settings in Plato
Plato's default FTN95 settings might have strict type checking enabled. Double-check that:
- You're passing arguments of the correct type (e.g., integer row indices, not real numbers)
- Your input matrix is indeed a 2D array (not 1D, which would cause a rank mismatch)
To get more details about the error, enable Verbose Error Messages in Plato:
- Go to
Project > Project Settings - Under
Compiler Options, check the box forVerbose - Recompile—this will show exactly which argument is causing the mismatch.
Final Check
Make sure your main program calls the subroutines with the right arguments. For example, if your matrix is declared as real :: eqn_matrix(3,4), your call should look like:
call swap(eqn_matrix, 1, 2) ! Swap rows 1 and 2 call scale(eqn_matrix, 2, 0.5) ! Scale row 2 by 0.5 call pivot(eqn_matrix, 1, 1) ! Pivot on element (1,1)
If you still hit issues, sharing the exact code for your subroutines and main program calls would help narrow things down further!
内容的提问来源于stack exchange,提问作者Matt Johnson

