Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arguments arent quite right
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Tue Aug 08, 2006 2:38 pm   Post subject: Arguments arent quite right

In my code i have a function that requires a parameter of type defense, however, when i try and pass something as type defense, all it gives me is an error : "Argument is wrong type"

Here's my code:

Turing:

class Shield
    type defense :
        record
            instance:^Shield
            hp, defs, lvl : int
            type_ : string
        end record

    %upgrades given shield
    function upgrade_shield (shield_ : defense, hp_rate, defense_rate : real) : defense
        var up_shield : defense
        up_shield.hp := round (shield_.hp * hp_rate)
        up_shield.defs := round (shield_.defs * defense_rate)
        up_shield.lvl := shield_.lvl + 1
        up_shield.type_ := shield_.type_
        shield := up_shield
        result up_shield
    end upgrade_shield
end Shield

type defense :
    record
        instance : ^Shield
        hp, defs, lvl : int
        type_ : string
    end record

var shield : flexible array 1 .. 0 of defense
new shield, upper (shield) + 1
new Shield, shield (1).instance

shield (1).hp := 20
shield (1).defs := 3
shield (1).lvl := 1
shield (1).type_ := "basic"

shield (1) := Shield (shield (1).number).upgrade_shield (shield (1), .5, .6)


if you try and run that you will see where the error occurs, is it because upgrade_shield is within a class? or is this possible and im just not doing it right?
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Tue Aug 08, 2006 4:16 pm   Post subject: Re: Arguments arent quite right

SuperFreak82 wrote:

Turing:

class Shield
    %upgrades given shield
    function upgrade_shield (shield_ : defense, hp_rate, defense_rate : real) : defense
        var up_shield : defense
        up_shield.hp := round (shield_.hp * hp_rate)
        up_shield.defs := round (shield_.defs * defense_rate)
        up_shield.lvl := shield_.lvl + 1
        up_shield.type_ := shield_.type_
        %****************
        shield := up_shield
        %****************
        result up_shield
    end upgrade_shield
end Shield



The highlighted line makes no sense at all. Functions return values (as in the proceeding line), they really shouldn't change any Outside World values.
In answer to your question, your parameter declarations:
code:

    function upgrade_shield (shield_ : defense, hp_rate, defense_rate : real) : defense

% Should be:

    function upgrade_shield (var shield_ : defense, hp_rate, defense_rate : real) : defense



Note the inclusion of 'var'. This is only if you wish to maintain your redundant assignment in the rest of your function. Otherwise, remove that and everything should be fine and dandy!
Good to see some Classes though.

Disclaimer: I haven't actually run the code...I'm just going off what I see right now. I don't have Turing at hand to be able to test it anyway.
Cervantes




PostPosted: Tue Aug 08, 2006 4:28 pm   Post subject: (No subject)

Whoa. You're trying to use objects, but you're using external types to store the data. Store it in the object.

You can get rid of that 'defense' type in the main level of your code (not within the class). It contains two things: 1) a pointer to the object which should contain all the data, and 2) the data that should be in the object. So really, you only need the ^Shield data type, and that's primitive.

Now, your defense type in your class can be modified. First, we can remove the 'instance' field. Why? It seems like it could be a good thing to have. The reason we can remove it is because we have the keyword, self, that implicitly is the pointer to the current object.

Next, we could easily remove the type altogether and just use ordinary variables like we learned them to be when we first started programming. Types are nice for putting variables in a container, but in this case, that's unnecessary. It is unnecessary because our variables are already within the container that is our object. If we put our variables in a type, and that type is in the object, then it's like wrapping a tiny Christmas present in larger and larger boxes just to irritate and surprise.

Next stop is to run your code. I get errors that I shouldn't have to deal with.
code:
shield := up_shield

shield is not defined.

code:

shield (1) := Shield (shield (1).number).upgrade_shield (shield (1), .5, .6)

number is not a field of shield (1).

Furthermore, I don't know what this code is supposed to be doing. Turing doesn't have constructors of this sort.


I suspect the error that you were getting has something to do with the fact that you've declared the defense type twice. Declaring and defining it twice, in and of itself, should scream bloody-murder in your ear. Follow the DRY principle: Don't Repeat Yourself.

If you really wanted to have that type both outside and inside the class, just declare it outside the class and import it. Similarly, you could declare it outside the class, but this time declare it as pervasive. That means it will automatically be imported into all your classes/modules.
Clayton




PostPosted: Tue Aug 08, 2006 5:53 pm   Post subject: (No subject)

sorry thats got some bits and pieces of my code in it, heres almost all of it (all that pertains to my problem anyways)

Turing:

class Shield
    export initialize, display_traits, upgrade_shield, update_hp, calculate_hp_loss

    type defense :
        record
            number : ^Shield
            hp, defs, lvl : int
            type_ : string
        end record

    var shield : defense

    %works
    procedure initialize (hp, defs, lvl : int, type_ : string)
        shield.hp := hp
        shield.defs := defs
        shield.lvl := lvl
        shield.type_ := type_
    end initialize

    procedure display_traits
        put shield.hp
        put shield.defs
        put shield.lvl
        put shield.type_
    end display_traits

    %upgrades given shield
    function upgrade_shield (shield_ : defense, hp_rate, defense_rate : real) : defense
        var up_shield : defense
        up_shield.hp := round (shield_.hp * hp_rate)
        up_shield.defs := round (shield_.defs * defense_rate)
        up_shield.lvl := shield_.lvl + 1
        up_shield.type_ := shield_.type_
        shield := up_shield
        result up_shield
    end upgrade_shield

    function update_hp (current_hp, hp_loss : int) : int
        shield.hp := current_hp - hp_loss
        result shield.hp
    end update_hp

    %calculates hp loss using factors of weapon power and type and shield defense and type
    function calculate_hp_loss (shield_type, wpn_type : string, wpn_power, shield_def : int) : int
        if shield_type = wpn_type then
            result round (wpn_power * .6 - shield_def)
        elsif shield_type ~= wpn_type and shield_type = "basic" then
            result round ((wpn_power + wpn_power * .2) - shield_def)
        else
            result wpn_power - shield_def
        end if
    end calculate_hp_loss

    function calculate_overflow (shield_hp, hp_loss : int) : int
        if shield_hp - hp_loss >= 0 then
            result 0
        else
            var overflow : int := shield_hp - hp_loss
            overflow := overflow - round (overflow * .66)
            result overflow
        end if
    end calculate_overflow
end Shield

type defense :
    record
        number : ^Shield
        hp, defs, lvl : int
        type_ : string
    end record

var shield : flexible array 1 .. 0 of defense
new shield, upper (shield) + 1
new Shield, shield (1).instance

shield (1).hp := 20
shield (1).defs := 3
shield (1).lvl := 1
shield (1).type_ := "basic"

Shield (shield (1).instance).initialize (shield (1).hp, shield (1).defs, shield (1).lvl, shield (1).type_)
Shield (shield (1).instance).display_traits
shield (1) := Shield (shield (1).instance).upgrade_shield (shield (1), 1.5, 1.6)
Shield (shield (1).instance).display_traits


sorry about the number thing, that was something from before that i forgot to take out Embarassed take a look at that and tell me what you think plz
Cervantes




PostPosted: Tue Aug 08, 2006 8:43 pm   Post subject: (No subject)

SuperFreak82 wrote:
take a look at that and tell me what you think plz

I glanced at this new one and I still see two copies of type defense.

...

Okay, now I ran it. You've got more errors, pertaining to the fact that you've removed the instance field from the defense type.

Once that defense type is fixed up (removed), show us where the error occurs. I suspect it's this line:

code:
shield (1) := Shield (shield (1).instance).upgrade_shield (shield (1), 1.5, 1.6)

The first parameter, shield (1), is causing the error. I believe this is because shield (1) is of the type defense, but this is the type that is in the outer level. The upgrade_shield method takes a parameter of the type defense, but this type is from inside the class. The two types know nothing of each other when they are created: the outer type doesn't know what goes on inside the class and the inner type can't know of the outer type because the outer type was not imported.

Remove the type and store the data only within the objects themselves. That will fix the error.

But there's more to this than the error. The whole code suggests an attempt to use OOP but clutching back to the old ideals of types. Your flexible array, shield, should be an array of pointers to the Shield class. In essence, this means that it will be an array of Shield objects. These Shield objects will internally store their data. They are also implicitly aware of who they are. You do not need to augment your objects' methods by passing, as a parameter, the object. Passing the object to itself? That's crazy-talk!
Clayton




PostPosted: Tue Aug 08, 2006 8:51 pm   Post subject: (No subject)

okay but how do i access the information i need that is inside of the class outside of it? thats what i dont understand...
Cervantes




PostPosted: Tue Aug 08, 2006 9:06 pm   Post subject: (No subject)

You export it.

But for most variables, you won't need to export them. The internal state of the object is managed entirely from within the object. It wouldn't be much of an internal state if it were managed externally.

Additionally, your object can interact with other objects through methods. See Part Two of the tutorial.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: