Fortran Coder

查看: 7293|回复: 5

[派生类型] 扩展数据类型多层嵌套,声明多个同类型变量后无法初始化

[复制链接]

3

帖子

1

主题

0

精华

新人

F 币
20 元
贡献
10 点
发表于 2015-12-1 21:30:24 | 显示全部楼层 |阅读模式
[Fortran] 纯文本查看 复制代码
module curveModule
    type point    !定义一个点
        integer x,y,z
    end type

    type curve   !定义一个曲线,有很多点构成
        type(point),dimension(:),allocatable::points
        character(:),allocatable::name
        contains
            procedure,pass,public::initCurve
    end type

    contains
        subroutine initCurve(self,psNum,ps,theName)
            class(curve)::self
            integer::psNum
            type(point),dimension(psNum)::ps
            character(len=*)::theName
            allocate(self.points(psNum))
            self.points=ps
            self.name=theName
        end subroutine
end module

module figureModule
    type figure !定义图表,有几条曲线构成
        type(curve)::c1,c2,c3

        contains
            procedure,pass::init
    end type
        contains
            subroutine init(self,inputFile)
                character(:),allocatable::inputFile
                class(figure)::self
                type(point)::p1=point(1,2,3),p2=point(2,3,4),p3=point(3,4,5),p4=point(4,5,6)
                type(point)::ps1(4),ps2(4)
                ps1=(\p1,p2,p3,p4\)
                ps2=(\p2,p3,p4,p1\)

                self.c1.init(size(ps1),ps1,'curve-1')
                self.c2.init(size(ps2),ps2,'curve-2') !若是只有才c1一个变量可执行,两个就不可,会覆盖第一个
                write(*,*) self.c1.name,self.c2.name
            end subroutine
end module

program main
    type(figure)::f
    call f.init('a.txt') !读入一个数据文件,将数据文件格式化的存成一个图表对象
end program

问题的描述:在图表的初始化里面不可以多次初始化曲线对象。若是将曲线放在数组里,
type(curves)::curves(8) !图表中的定义
在图表初始化的执行为:self.curves(1).init(size(ps1),ps1,'curve-1')
                                       self.curves(2).init(size(ps1),ps1,'curve-1')
这样程序可以顺利执行,但是程序的可读性很差,总是引用数组下标。更关键的为什么会这样??

790

帖子

2

主题

0

精华

大宗师

F 币
3765 元
贡献
2255 点
发表于 2015-12-1 22:33:56 | 显示全部楼层
你这个根本就不能编译通过,勿论其他。
[Fortran] 纯文本查看 复制代码
module curveModule
    type point   !定义一个点
        integer x,y,z
    end type

    type curve   !定义一个曲线,有很多点构成
        type(point),dimension(:),allocatable::points
        character(:),allocatable::name
        contains
            procedure,pass,public::initCurve
    end type

    contains
        subroutine initCurve(self,psNum,ps,theName)
            class(curve)::self
            integer::psNum
            type(point),dimension(psNum)::ps
            character(len=*)::theName
            allocate(self.points(psNum))
            self.points=ps
            self.name=theName
        end subroutine
end module

module figureModule
use curveModule
    type figure !!定义图表,有几条曲线构成
        type(curve)::c1,c2,c3

        contains
            procedure,pass::init
    end type
        contains
            subroutine init(self,inputFile)
                character(*) inputFile
                class(figure)::self
                type(point)::p1=point(1,2,3),p2=point(2,3,4),p3=point(3,4,5),p4=point(4,5,6)
                type(point)::ps1(4),ps2(4)
                ps1=(/p1,p2,p3,p4/)
                ps2=(/p2,p3,p4,p1/)

                call self.c1.initCurve(size(ps1),ps1,'curve-1')
                call self.c2.initCurve(size(ps2),ps2,'curve-2') !!若是只有才c1一个变量可执行,两个就不可,会覆盖第一个
                write(*,*) self.c1.name,self.c2.name
            end subroutine
end module

program main
use figureModule
    type(figure)::f
    call f.init('a.txt') !!读入一个数据文件,将数据文件格式化的存成一个图表对象
end program

3

帖子

1

主题

0

精华

新人

F 币
20 元
贡献
10 点
 楼主| 发表于 2015-12-1 23:23:19 | 显示全部楼层
首先感谢!
我明日再试一遍!

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

发表于 2015-12-2 08:57:24 | 显示全部楼层
剔除ivf的独家特性后,gfortran编译无问题,也没有发现lz覆盖的问题
至于多次初始化,势必先得判断allocated,不能上手就allocate
实在想不通为啥非要在Fortran上折腾OOP功能,关键很多轮子得自己造,效率还不一定高

3

帖子

1

主题

0

精华

新人

F 币
20 元
贡献
10 点
 楼主| 发表于 2015-12-2 10:21:08 来自移动端 | 显示全部楼层
重复执行还是不行。我怀疑编译器,开发环境了!!!
IMG_20151202_101649.jpg
IMG_20151202_101955.jpg

1948

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1298 元
贡献
547 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2015-12-2 11:59:07 | 显示全部楼层
你的代码经过竹叶的修改,可以正确运行,并没有发现覆盖问题。
输出为: curve-1curve-2

此外,你后来提供的截图太小了。论坛允许2MB的图片附件,应该够用。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-28 22:38

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表