文字配列に新しい文字を追加するクラスプロシージャを作成しようとしていますが、GFortranでコンパイルする際に "配列のコンストラクタの文字数が異なる"文字の長さがわかっていても、それは同じです。Fortranで文字配列に追加する
はここに私の機能です:
subroutine addToArray(this, newElement)
class(MyClass), intent(inout) :: this
character(len=*), intent(in) :: newElement
character(len=256) :: tempElement
character(len=256), dimension(:), allocatable :: temp
tempElement = newElement ! Needed otherwise newElement is of the wrong size
allocate(temp(size(this%charArray)+1) ! Make the char array bigger by 1
temp = [this%charArray, tempElement]
call move_alloc(from=temp, to=this%charArray)
end subroutine
これはエラーFortran runtime error: Different CHARACTER lengths (538976288/256) in array constructor
になります。しかし、len(this%charArray)
またはlen(tempElement)
を印刷すると、長さは256文字になります。では538976288はどこから来ていますか?
私は通常、この手順をmyObject%addToArray('hello')
のようなものを使用して呼び出しています。 this%charArray
は型定義でcharacter(len=256), dimension(:), allocatable :: charArray
と宣言され、allocate(this%charArray(0))
を使用して割り当てられます。
コンパイラのバグかもしれません。どのgfortranのバージョンですか? MyClassのcharArrayコンポーネントの宣言を推測していることに注意してください。 Fortran 2003では 'this%charArray = [this%charArray、tmpElement]'が許可されていることに注意してください。 – IanH
gfortranなら、このようなバグは非常に多くあります。バグデータベースを検索するだけです。これらのバグを制限する1つの方法は、固定長の固定文字を一貫して使用することです。 –
GFortranバージョン6.30。 charArrayは 'character(len = 256)、dimension(:)、allocatable :: charArray'のように宣言され、' allocate(this%charArray(0)) 'で割り当てられます。 –