Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

possible fundamental error

This topic is 5911 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey folks - im a relative newbie to c/c++/games programming/dx, but been doing Ada for a year at first year uni so i am comfortable with programming principles. I''m creating the base logic system for an rpg, and have a problem with the code... I''ve got classes representing monsters, weapons, armour, and the processes involved in a fight between 2 monsters (the player and another monster - atm, the player is treated as a monster) class cFight { public: static float BattlePartResult; //static, so it can be used in the calling statement inline float fWeaponToWeaponScore(cMonster mAttacker, cMonster mDefender) { BattlePartResult = ( (mAttacker.mAttackRating + rand()%6) - (mDefender.mAttackRating + rand()%6) ); //check whether the players attack has //gotten thru weapon defence return BattlePartResult; } //basically this makes gets the attack rating,adds a random //number from 1-6, and compares it to the enemies attack //rating - this is to simulate the parry and 3 other functions similar to this one in that class, used to resolve combat by being called as follows if ( cFight::fWeaponToWeaponScore(mHuman, mOrc) > 0) //if the //attacker got through enemies weapon //then { if (cFight::fWeaponToDefenceScore(mHuman, mOrc) > 0) //if //the weapon and strength of the //attacker was enough to wound the //target { //again, target is only checked for damage if the //target''s weapon and defence are defeated cFight::fDamageDealt(mHuman, mOrc); //damage dealt by //Human on Orc } } Can anyone spot the error here - if more info is needed just say, as i need this problem solved asap, so i can be sure I''m doing the right thing Peasant:>"Help Help Im being repressed!" King Arthur:>"Bloody Peasant!"

Share this post


Link to post
Share on other sites
Advertisement
oh and the error message is as follows:

''cFight::fWeaponToWeaponScore'' : illegal call of non-static member function

Peasant:>"Help Help Im being repressed!"

King Arthur:>"Bloody Peasant!"

Share this post


Link to post
Share on other sites
And the reason for the error:

You declared fWeaponToWeaponScore as a non static method. This means that it only exists in association with an object of type cFight.

However, you called fWeaponToWeaponScore as a static method. That is you didn''t call it on a cFight object.

static class fields or methods are called without an object because the same field or method exists for all objects of the class (eg/ ClassName::methodName()).

Non-static fields or methods can only be accessed through an object of the class (eg/ objectName.methodName()).

hope the random waffle helps...
DeVore

Share this post


Link to post
Share on other sites
cheers devore....
So what ure saying is:
static class types must have their member functions called by a variable of the class type and not just a generic call yes??

could you explain to me why?

That got me past that error, though ive got a linking error now; an unresolved external symbol, but it shouldnt prove too much bother - i expect ive forgotten to #include something

******************************
Peasant:>"Help Help Im being repressed!"

King Arthur:>"Bloody Peasant!"

Share this post


Link to post
Share on other sites
not exactly sutek,

class = like a blueprint (think of it like a description of your object/s). Contains methods and fields but doesn't actually exist until you create an instance of it.

instance of class = object

to use stuff in your class (to call methods or look at fields), you need an object of that class.

static methods or fields are the exception. You don't need an object to call or look at them. You can call or look at them using the class name.

ClassName::methodName() = an example of calling a static method of the class called ClassName.

objectName.methodName() = an example of calling a non static method on an object called objectName.

-DeVore

Edited by - DeVore on February 14, 2001 10:12:42 AM

Share this post


Link to post
Share on other sites
hmm... I always thought you could call class functions using
only the class-name, ie. Class::DoSomething()

However, it didn''t work when I tried.

How woul''d you get a class member like this to be "callable" with only the class name?

static const real Vector::Add (const Vector& Point, const Vector& Point2, float *something) const;


the compiler generates this error:

error C2272: ''Add'' : modifiers not allowed on static member functions

Share this post


Link to post
Share on other sites
quote:
Original post by FXO

static const real Vector::Add (const Vector& Point, const Vector& Point2, float *something) const;


It''s the const after the function declaration that is an invalid modifier. Since a static function is not associated with any particular object, declaring it to be a const member function is contradictory (it''s not a member function).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by FXO
hmm... I always thought you could call class functions using
only the class-name, ie. Class::DoSomething()

However, it didn''t work when I tried.

How woul''d you get a class member like this to be "callable" with only the class name?

Do it exactly as you wrote, but only for static members. The whole point of non-static members is that they operate on an instance of the class. Calling that function without an instance to operate on is nonsensical.



[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!