• Advertisement

Archived

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

possible fundamental error

This topic is 5820 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
quote:

Calling that function without an instance to operate on is nonsensical.



Yeah, but it could be useful to have static class-functions when you wanna keep e.g. all your vector operations in a Vector-Class.

You coul''d make an overload of Vector::Add() that only takes external parmeters and make it static.


  
Vector::Add(Vector &V)
//...Add V to (*this vector)


const Vector Vector::Add(Vector &V1, Vector &V2)
//...Add V1 to V2 and return the result

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
FXO, what would make more sense, rather than making a separate class for operations would be to have them in the vector class itself, which would save you some considering they would have one of the parameters implied, and it makes polymorphism easier to conrol for different sub-vector classes.

Sutek --
I think the big problem with your class cFight isn''t your syntax. While you can use classes in the way that you are attempting, you seem to be missing the actual concept of what a class is. When you make an instance of a class, it should conceptually be as an object, like their names suggest. Those methods have operations that affect their datamembers and interaction with other classes/global data. Having a class devoted to fighting doesn''t really make much sense, as you''d have to pass in 2 monster parameters, etc. and because many of the methods would have to be static, you''re pretty much using the class as a grouping of functions, which really serves no purpose, and making instances of that class would be pointless.

A better approach would be to have an abstract base class devoted to a generic monster which contains prototypes and pure virtual methods devoted to "attack" which would take one parameter (the object it would be attacking), "defend" (which would put the monster into defend mode for one turn, move (which would move the monster), etc. Each TYPE of monster would be a class derived from that ABC, and everytime you wanted to make one of those enemies, you''d make an instance of that class. With polymorphism you can redefine the attack, defend, move, etc. functions on each derived member in the hierarchy for the associated monster. Also, since they share a common ABC, you can store them all in like terms via a pointer/array of pointers/linked list of pointers etc. to an instance of an ABC, and be able to manage all of your monsters as though they were one type, but still be able to have their virtual methods each uniquely defined. If you are ever unsure of what to use as a class, just think "does this make sense conceptually as an object?" Fighting is more of an action with varying members, so it would better be left as a method in a more general class, IE the moster class as I mentioned above. Object-Oriented programming is a lot like how you''d percieve the world as items, organisms, etc. If you can percieve it as an object in real life, then it probably makes sense as an object in C++. Otherwise, with many exceptions, you should probably re-evaluate the situation.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
( I was agreeing with FXO there in the first line, but i think my wording was a little messy)

Share this post


Link to post
Share on other sites
quote:

rather than making a separate class for operations would be to have them in the vector class itself,



That's what I was trying to say

btw. Is there anyway to get notified when somebody post on a thread that you didn't start?

(Something similar to the
[X] Check here to be notified by email whenever someone replies to your topic - button)

[edited by - FXO on March 16, 2002 8:01:10 AM]

Share this post


Link to post
Share on other sites

  • Advertisement