Declaring an object outside if statement?
Author |
Message |
Luffy123
|
Posted: Sun May 13, 2012 3:26 pm Post subject: Declaring an object outside if statement? |
|
|
I have run into a problem where it says obj isn't recognized im guessing its cuz i declared the var obj in an if statement. so I'm wodnering how I can decalre it outside and if I eve ncan?
import Heart in "C:/Users/Hamza/Desktop/classes/classes/heart.tu", Diamond in "C:/Users/Hamza/Desktop/classes/classes/diamond.tu"
setscreen ("graphics:640;480,nocursor")
var objX, objY, objHeight, objWidth, objColour : int
var objName : string
put "Do you want a diamond or a heart?"
get objName
put "Enter the x coordinate of the heart."
get objX
put "Enter the y coordinate of the heart."
get objY
put "Enter the height of the heart."
get objHeight
put "Enter the width of the heart."
get objWidth
put "Enter the colour of the heart."
get objColour
if objName = "heart" then
var obj : pointer to Heart
new obj
end if
if objName = "diamond" then
var obj : pointer to Diamond
new obj
end if
obj -> setCentre (objX, objY)
obj -> setHeight (objHeight)
obj -> setWidth (objWidth)
obj -> setColour (objColour)
obj -> draw
I wanna save code so I'm wondering how I can do this. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Sun May 13, 2012 3:50 pm Post subject: Re: Declaring an object outside if statement? |
|
|
Every class in Turing inherits from the anyclass class so we can write the following:
Turing: | var flag: string
var obj : ^ anyclass
if flag = "class1" then
new class1, obj
elsif flag = "class2" then
new class2, obj
.. .
|
|
|
|
|
|
![](images/spacer.gif) |
Luffy123
|
Posted: Sun May 13, 2012 3:56 pm Post subject: Re: Declaring an object outside if statement? |
|
|
Dreadnought @ Sun May 13, 2012 3:50 pm wrote: Every class in Turing inherits from the anyclass class so we can write the following:
Turing: | var flag: string
var obj : ^ anyclass
if flag = "class1" then
new class1, obj
elsif flag = "class2" then
new class2, obj
.. .
|
I don't understand why it isn't working?
var obj : pointer to anyclass
if objName = "heart" then
new Heart, obj
end if
if objName = "diamond" then
new Diamond, obj
end if
obj -> setCentre (objX, objY)
obj -> setHeight (objHeight)
obj -> setWidth (objWidth)
obj -> setColour (objColour)
obj -> draw |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Sun May 13, 2012 4:26 pm Post subject: Re: Declaring an object outside if statement? |
|
|
Remember that obj is a pointer to anyclass not to diamond or hearts. Therefore, Turing can't be sure that setCentre, setheight exist for the type of object that obj will point to (obj could point to some other class). It can only be sure that things in the export list of anyclass exist, but setCentre and setHeight are in the export list of a different class.
You can either use a different syntax: Turing: | class class1
export var a
var a : int
end class1
var obj : ^ anyclass
new class1, obj
class1 (obj ).a := 4
put class1 (obj ).a |
Or, if you understand how inheritance works, you can make a class from which your hearts and diamonds will inherit functions that are common to both (and then you can use these functions with the '->' operator.)
Hope this helps. |
|
|
|
|
![](images/spacer.gif) |
Raknarg
![](http://compsci.ca/v3/uploads/user_avatars/3745510004d8be6689b92f.jpg)
|
Posted: Sun May 13, 2012 6:01 pm Post subject: RE:Declaring an object outside if statement? |
|
|
I think that you shouldn't keep these as separate objects, like you appear to be doing. They're technically the same object, the only difference being the suit. At least have different subclasses of the same class (with inheritance) |
|
|
|
|
![](images/spacer.gif) |
|
|