为什么尝试附加后会弹出错误?
Why does an error pop up after trying to append?
table1 = {1,2,3,4,5}
table2 = {1,2,3,4}
metatable ={__add = function(table,otherTable)
sumTable = {}
for i=1, #table do
table.insert(sumTable,table[i])
end
for i=1, #otherTable do
table.insert(sumTable,otherTable[i])
end
return sumTable
end
}
setmetatable(table1,metatable)
table1 = table1 + table2
运行程序后弹出此错误
lua: [string ""]:6: attempt to call field 'insert' (a nil value)
stack traceback:
[string ""]:6: in function <[string ""]:3>
[string ""]:16: in main chunk
您的 __add
代码使用 table
作为隐藏 table
库的参数。将参数重命名为其他名称。
table1 = {1,2,3,4,5}
table2 = {1,2,3,4}
metatable ={__add = function(table,otherTable)
sumTable = {}
for i=1, #table do
table.insert(sumTable,table[i])
end
for i=1, #otherTable do
table.insert(sumTable,otherTable[i])
end
return sumTable
end
}
setmetatable(table1,metatable)
table1 = table1 + table2
运行程序后弹出此错误
lua: [string ""]:6: attempt to call field 'insert' (a nil value) stack traceback: [string ""]:6: in function <[string ""]:3> [string ""]:16: in main chunk
您的 __add
代码使用 table
作为隐藏 table
库的参数。将参数重命名为其他名称。