2016-05-22 4 views
2

質問はtorch5チュートリアルからです:Luaの、インデックスフィールドへの試み '__parent'(ゼロ値)

/Users/frankhe/torch/install/bin/luajit: test1.lua:39: attempt to index field '__parent' (a nil value) 
:このスクリプトを実行した後 http://torch5.sourceforge.net/manual/torch/index-8-1.html

require "torch" 

-- for naming convenience 
do 
    --- creates a class "Foo" 
    local Foo = torch.class('Foo') 

    --- the initializer 
    function Foo:__init() 
    self.contents = "this is some text" 
    end 

    --- a method 
    function Foo:print() 
    print(self.contents) 
    end 

    --- another one 
    function Foo:bip() 
    print('bip') 
    end 

end 

--- now create an instance of Foo 
foo = Foo() 

--- try it out 
foo:print() 

--- create a class torch.Bar which 
--- inherits from Foo 
do 
    local Bar = torch.class('torch.Bar', 'Foo') 

    --- the initializer 
    function Bar:__init(stuff) 
    --- call the parent initializer on ourself 
    self.__parent.__init(self) 

    --- do some stuff 
    self.stuff = stuff 
    end 

    --- a new method 
    function Bar:boing() 
    print('boing!') 
    end 

    --- override parent's method 
    function Bar:print() 
    print(self.contents) 
    print(self.stuff) 
    end 
end 

--- create a new instance and use it 
bar = torch.Bar("ha ha!") 
bar:print() -- overrided method 
bar:boing() -- child method 
bar:bip() -- parent's method 

、私はエラーメッセージが表示されました

詳細画像は次のとおりです。 enter image description here

このエラーが発生した理由を知りたいと思います。

答えて

1

用途:

local Bar, parent = torch.class('torch.Bar', 'Foo') 

そして:

function Bar:__init(stuff) 
    parent.__init(self) 

    self.stuff = stuff 
end 
+0

これは、問題を解決します。しかし、なぜあなたは「自己__親」がうまくいかないのか教えていただけますか?ありがとうございました! –

+1

これはtorch7クラスシステムの仕組みです:この[documentation](https://github.com/torch/torch7/blob/master/doc/utility.md)を参照してください。そのような '__parent 'フィールドはありません。代わりに、['torch.class'](https://github.com/torch/torch7/blob/master/init.lua#L104-L107)によって返される親クラスmetatableと直接対話します。 – deltheil

関連する問題