Why is this not working?
BlitzMax Forums/BlitzMax Beginners Area/Why is this not working?
| ||
When building the code below, I keep getting an error saying: Compile Error Unable to create new object of abstract "Type" With no debugger, I'm clueless as to what I'm doing wrong. I am new to all this OOP stuff, but from what I've learned so far my code should be fine. Could someone enlighten me? :) 'CREATE GLOBAL LIST FOR SPRITE Global SpriteList:TList = CreateList() 'OBJECT: '--- A simpler way to work with Images Type tSprite '--- SPRITE PROPERTIES --- Field Image:TImage Field Mode:Int Field ScaleX:Float Field ScaleY:Float Field Angle:Float Field Alpha:Float Field Tint:Int [3] '------------------------- '--- SPRITE CONSTRUCTOR --- Function Create:tSprite (_Image:TImage, _Mode:Int = SOLIDBLEND) '--- CREATE NEW SPRITE Local o:tSprite = New tSprite '--- INITIATE SPRITE WITH DEFAULTS o.Image = _Image o.Mode = _Mode o.ScaleX = 1.0 o.ScaleY = 1.0 o.Angle = 0.0 o.Alpha = 1.0 o.Tint = [255,255,255] '--- ADD NEW SPRITE TO LIST SpriteList.AddLast(o) '--- RETURN CREATED OBJECT Return o End Function '-------------------------- '--- RENDER METHOD --- Method Render (_X:Float, _Y:Float, _Frame:Int = -1) '--- CREATE TEMPORARY VARIABLES Local _ScaleX:Float Local _ScaleY:Float Local _Angle:Float Local _Alpha:Float Local _Mode:Int Local _Tint:Int [3] '--- STORE CURRENT SETTINGS _Angle = GetRotation () _Alpha = GetAlpha () _Mode = GetBlend () GetScale _ScaleX,_ScaleY GetColor _Tint[0],_Tint[1],_Tint[2] '--- SET OBJECTS SETTINGS SetScale ScaleX, ScaleY SetRotation Angle SetAlpha Alpha SetBlend Mode SetColor Tint[0], Tint[1], Tint[2] '--- SET FRAME If _Frame = -1 Then _Frame = Frame '--- DRAW IMAGE DrawImage Image, _X, _Y, _Frame '--- RESTORE SETTINGS SetScale _ScaleX, _ScaleY SetRotation _Angle SetAlpha _Alpha SetBlend _Mode SetColor Tint[0], Tint[1], Tint[2] End Method '----------------------- Method Update() Abstract End Type |
| ||
Ah. You have an abstract method in there which makes it an abstract type. You can't create a sprite as you are trying to do in the create function because you can't create an abstract type. |
| ||
Thanks for your reply. Are you sure that's the problem? If I completely take out the abstract method, it still does not compile. |
| ||
Right, abstract is saying that you want other types to derive from this one and that those derived types MUST implement your abstract functions. You can't instantiate any class that has a missing or declared abstract method. And FYI, a debugger wouldn't be of any use here as this is a compile error... |
| ||
Why would ou want anything to derive from sprite anyway? You should just make the update method a normal method. Also, when something "does not compile" It's handy to post the error message, and prefereably the line number, local to the top of the code snippet |
| ||
@ WarrenM Thanks for the clarification, I think I get abstract methods better now. But even without any abstract reference I still get a "compile error" :) [edit] Actually you guys are right it was that I was abstracting tSprite. Still I wonder why your not allowed to create an instance of an abstract class. @ Bot Builder Your right I should have posted what line number the error pointed to. I did however post the error message. But to save anyone the time of reading previous posts the error and location are: LINE 23< CHAR 3: "Local o:tSprite = New tSprite" Compile Error Unable to create new object of abstract "Type"As for the reason why I wanted to use tSprite as a base class; I wanted to create a tAnimatedSprite class that derived from the tSprite class. Am I going about this all wrong? Should static and animated sprites be in the same class? Thanks for your help guys! |
| ||
You can't create an abstract class because if its abstract, then that means that some of its elements are undefined. That's the point of an absract class. Some elements are udefined so you define them later in an inheriting class. If the class is not abstract you can still override the definition of a method in a child class, and create a child or base class. |