LUA中Attempt to call a non-callable object或attempt to call field ‘new’ (a nil value)问题的解决

  • Post author:
  • Post category:其他


在使用tolua++将c++函数导出,可以被lua调用之后,编译通过,但会报类似这样的错误:

1,attempt to call field ‘new’ (a nil value)

在使用local aaa = MyClass:new()时会报这样的错误

2,Attempt to call a non-callable object

在使用local aaa = MyClass()时会这么报

原因是因为MyClass的pkg文件中没有声明构造函数。

分析:我粗略地理解如下:

没有在pkg中声明->导致c++与lua的连接类没有这个构造函数->导致在local aaa = MyClass()时,编译器认为你是想直接用这个类(不通过构造函数)

->所以告诉你他不可被调用

不知道这么理解是否准确,请路过的大神指点。



版权声明:本文为bark2003原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。