About Monkey 2 › Forums › Monkey 2 Programming Help › Need help converting monkey 1 to monkey 2 code.
This topic contains 10 replies, has 3 voices, and was last updated by 
 Jesse
 2 years, 9 months ago.
- 
		AuthorPosts
 - 
		
			
				
June 30, 2016 at 8:10 pm #1423
I am trying to convert this code to monkey 2 but I can’t figure out how to do it correctly it works in monkey 1:
[/crayon]Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950[crayon-5cba87f315338877711170 inline="true" ]'*******************************************************'* Store *'*******************************************************Class Store<T>Field last:TField total:IntMethod New()End MethodMethod New(count:Int)Fill(count)End MethodMethod Fill:Void(total:Int)For Local i:Int = 0 Until totalLocal c:T = New T()c._pred = lastlast = cNextSelf.total = totalEnd MethodMethod Count:Int()Return totalEnd MethodMethod GetItem:T()If lastLocal c:T = lastlast = Cast(last._pred)c._pred = Nulltotal -= 1Return cEndifReturn New T()End MethodMethod ReturnItem:Void(obj:T)obj._succ = Nullobj._pred = lastlast = objtotal += 1End MethodEnd ClassI get this errors trying to compile it:
/Users/legend/Desktop/patrol 2/store.monkey2 [32] : Error : Identifier ‘T’ not found
/Users/legend/Desktop/patrol 2/store.monkey2 [38] : Error : Can’t find overload for ‘new(…)’ with argument types ()
/Users/legend/Desktop/patrol 2/store.monkey2 [29] : Error : Missing return statement
/Users/legend/Desktop/patrol 2/store.monkey2 [18] : Error : Can’t find overload for ‘new(…)’ with argument types ()
/Users/legend/Desktop/patrol 2/store.monkey2 [19] : Error : Identifier ‘c’ not found
/Users/legend/Desktop/patrol 2/store.monkey2 [20] : Error : Identifier ‘c’ not found
//Users/legend/Desktop/patrol 2/store.monkey2 [9] : Error : Missing return statement
/Users/legend/Desktop/patrol 2/store.monkey2 [32] : Error : Identifier ‘T’ not found
/Users/legend/Desktop/patrol 2/store.monkey2 [38] : Error : Can’t find overload for ‘new(…)’ with argument types ()
/Users/legend/Desktop/patrol 2/store.monkey2 [29] : Error : Missing return statement
/Users/legend/Desktop/patrol 2/store.monkey2 [18] : Error : Can’t find overload for ‘new(…)’ with argument types ()
/Users/legend/Desktop/patrol 2/store.monkey2 [19] : Error : Identifier ‘c’ not found
/Users/legend/Desktop/patrol 2/store.monkey2 [20] : Error : Identifier ‘c’ not foundstill trying to figure out how to post code here.
June 30, 2016 at 10:49 pm #1439Are you wanting T to be a generic type? I think you’re just missing the <T> after the class declaration..
Monkey1Class Store<T>June 30, 2016 at 11:25 pm #1442I don’t know how that got removed. I do have it on my original code. apparently in the process of figuring out how to post code here it got removed. So the error happen with the “<T>” in place.
July 1, 2016 at 12:48 am #1447Explicit casting in MX2 is different than MX1. This line:
last = T(last._pred)
Should be:
last = Cast<T>(last._pred)
The following should compile without errors (I haven’t checked for any logic errors though):
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758[crayon-5cba87f31eaf2318920743 inline="true" ]#import "<std>"Function Main:Void()local store := New Store<Item>(20)EndClass ItemField _pred:ItemField _succ:ItemEndClass Store<T>Field last:TField total:IntMethod New(count:Int)Fill(count)End MethodMethod Fill:Void(total:Int)For Local i:Int = 0 Until totalLocal c:T = New T()c._pred = lastlast = cNextSelf.total = totalEndMethod Count:Int()Return totalEndMethod GetItem:T()If lastLocal c:T = lastlast = Cast<T>(last._pred)c._pred = Nulltotal -= 1Return cEndifReturn New T()EndMethod ReturnItem:Void(obj:T)obj._succ = Nullobj._pred = lastlast = objtotal += 1EndEndJuly 1, 2016 at 1:05 am #1450thanks Impixi. I was wondering about the casting thing. that solved several errors. but I still get these errors (on my code):
/Users/legend/Desktop/patrol 2/store.monkey2 [38] : Error : Can’t find overload for ‘new(…)’ with argument types ()
/Users/legend/Desktop/patrol 2/store.monkey2 [29] : Error : Missing return statement
/Users/legend/Desktop/patrol 2/store.monkey2 [18] : Error : Can’t find overload for ‘new(…)’ with argument types ()
/Users/legend/Desktop/patrol 2/store.monkey2 [19] : Error : Identifier ‘c’ not found
/Users/legend/Desktop/patrol 2/store.monkey2 [20] : Error : Identifier ‘c’ not foundJuly 1, 2016 at 1:25 am #1452What’s your code for creating a new Store instance?
July 1, 2016 at 1:35 am #1454it uses this as a base for all my objects:
Monkey1234567891011121314151617181920Class StoreObjectField _pred:StoreObjectField _succ:StoreObjectFunction Separate:Void(obj:StoreObject)If obj._predIf obj._succobj._pred._succ = obj._succobj._succ._pred = obj._predElseobj._pred._succ = NullEndifElseif obj._succobj._succ._pred = NullEndifEnd FunctionEnd ClassI create instance of it the same way you did in your example. it works fine in monkey1 but I can’t figure this errors out.
July 1, 2016 at 2:52 am #1476If you’re making use of inheritance there are issues relating to constructor “chaining” – might be related to your errors?
There’s a forum topic about it:
http://monkey2.monkey-x.com/forums/topic/referencing-new/You might need to rework your underlying architecture a little bit to make it play nice with MX2…
July 1, 2016 at 2:59 am #1479I don’t think that’s the issue. the problem seems to be when creating an instance of generic such as:
</p><p>new T().</p><p>
July 1, 2016 at 3:13 am #1481well I isolated the code and it seems to work fine. it has to be from other files that are accessing it.
Thanks Impixi for your help.
July 6, 2016 at 5:11 pm #1790Well, I kept getting an error when create stores of new classes and I couldn’t figure out what the problem was until now. it appears that I have to create a simple new method on the “T” class or it fails to compile. I am posting this for anybody that runs into this problem.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.