我在haXe的编程新的,我建我的程序它输出:如何解决haxe程序中的成员变量初始化错误?
Member variable initialization is not allowed outside of class constructor
有谁知道如何解决这个问题,在不改变初始化数据成员?
我在haXe的编程新的,我建我的程序它输出:如何解决haxe程序中的成员变量初始化错误?
Member variable initialization is not allowed outside of class constructor
有谁知道如何解决这个问题,在不改变初始化数据成员?
首先,如果您告诉我们您使用的是哪种版本的Haxe,以及您有哪些源代码会产生错误,最好以尽可能最小的形式提供帮助,这可能会有所帮助。
我这样说的原因是最新的Haxe版本(3.0.1)我相当确信永远不会产生确切的错误信息......除非我错了:)所以很难知道你的版本是什么正在使用并且很难知道问题可能是什么。
我的猜测:您正在使用不允许的成员变量的初始化。在Haxe的旧版本中,它完全不被允许,在Haxe 3中它只允许“恒定”值(字符串,整数等)。我在Haxe 3中收到错误消息“变量初始化必须是一个常数值”,但错误消息可能在版本之间发生了变化。
断码
class Initialization
{
static function main() {
new Initialization();
}
var myInt = 0;
var myString = "some string";
var myArray = [1,2,3]; // Error: "Variable initialization must be a constant value"
public function new() {
trace(myInt);
trace(myString);
trace(myArray);
}
}
工作代码
class Initialization
{
static function main() {
new Initialization();
}
var myInt = 0;
var myString = "some string";
var myArray:Array<Int>; // Define the type, but don't initialize here
public function new() {
myArray = [1,2,3]; // Initialize in the constructor
trace(myInt);
trace(myString);
trace(myArray);
}
}
编辑:哦,你在HAXE 2.09。没有内嵌初始化为您服务;)
class Initialization
{
static function main() {
new Initialization();
}
// Define the type, but don't initialize here
var myInt:Int;
var myString;
var myArray:Array<Int>;
public function new() {
// Initialize in the constructor
myInt = 0;
myString = "some string";
myArray = [1,2,3];
trace(myInt);
trace(myString);
trace(myArray);
}
}
http://haxe.org/manual/basic_types#impact-on-cross-platformability – W55tKQbuRu28Q4xv