OOP – Lua – Try calling method ‘new’ (zero value)

Lua newbie, trying to figure out how to use middleclass library for OOP

main.lua:

< pre>require’middleclass’
require’Person’

local testPerson = Person:new(“Sally”); //causes Runtime error: attempt to call method’new’ (a nil value)
testPerson:speak();

Person.lua:

module(..., package.seeall)
require'middleclass'

Person = class('Person');
function Person:initialize(name)
self.name = name;
print(" INITIALIZE: ".. self.name);
end

function Person:speak()
print('Hi, I am '.. self.name ..'. ')
end

Why do I receive this error?

First of all, the semicolon at the end of the line is not necessary, it may be a bad habit of writing Lua code. Secondly, I changed the requirement that’middleclass’ needs’middleclass.init’ in both files and deleted the module (…, package.seeall). After that, the sample code works fine with Lua 5.1.4 on my machine.

main.lua

require'Person'

local testPerson = Person:new("Sally")
testPerson:speak()

Person.lua

require'middleclass.init'

Person = class ('Person')

function Person:initialize(name)
self.name = name
print("INITIALIZE: ".. self.name)
end

function Person:speak()
print('Hi, I am '.. self.name ..'.')
end

You The middleclass.lua file may be included directly. There is no setting to work in this way. The purpose is to include middleclass/init.lua.

If you use these two files exactly as shown above and layout as shown below File, you can use.

./main.lua
./Person.lua
./middleclass/init.lua
. /middleclass/middleclass.lua

Novice to Lua, trying to figure out how to use middleclass library for OOP

main.lua:

require'middleclass'
requir e'Person'

local testPerson = Person:new("Sally"); //causes Runtime error: attempt to call method'new' (a nil value)
testPerson:speak( );

Person.lua:

module(..., package.seeall)
require'middleclass'
< br />Person = class('Person');
function Person:initialize(name)
self.name = name;
print("INITIALIZE: ".. self.name);
end

function Person:speak()
print('Hi, I am '.. self.name ..'.')
end

Why do I receive this error?

First of all, the semicolon at the end of the line is not necessary, it may be a bad habit of writing Lua code. Secondly, I changed the requirement of’middleclass’ in two The file needs’middleclass.init’ and the module (…, package.seeall) is deleted. After that, the sample code works normally on my machine using Lua 5.1.4.

main. lua

require'Person'

local testPerson = Person:new("Sally")
testPerson:speak()

Person.lua

require'middleclass.init'

Person = class('Person')

function Person:initialize(name)
self.name = name
print("INITIALIZE: ".. self.name)
end

function Person:speak ()
print('Hi, I am '.. self.name ..'.')
end

You may directly include the middleclass.lua file. There is no setting to this Work in this way. The purpose is to include middleclass/init.lua.

If you use these two files exactly as shown above and lay out the files as shown below, you can use it.

./main.lua
./Person.lua
./middleclass/init.lua
./middleclass/middleclass.lua

Leave a Comment

Your email address will not be published.