2013-09-29 21 views

回答

1

首先,如果您告诉我们您使用的是哪种版本的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); 
    } 
} 
+0

我创造的FlashDevelop项目与NME 3.3.5和HAXE 2.09。我仍然约会同样的错误。我的程序与您放置的示例类似。我无法初始化数据成员。问候 – kelvincer

+0

嗨,Haxe 2.09不支持在构造函数外初始化成员变量。 [changelog](http://haxe.org/file/CHANGES.txt)表示它在2.10中添加了。这是一个相当直接,不会突破到2.10的升级 - 我会推荐它。否则,你将不得不在构造函数中初始化你的所有值。我会用示例代码更新我的答案。 –

+0

感谢您的回答。 – kelvincer

相关问题