| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Inheritance, Polymorphism, and Composition
This has been holding me back from the next big step in a project I administrate called GameServ.
Basically it's an RPG game with the following objects: aClient (irc person), Player (an irc client's stats), Monster (a monster to fight), and I'm adding a new set of classes with base class item. Here's my problem... item is the base class of a chain of inheritant sub-classes that have polymorphic functions which I want to use for different modifiers within each item. I have the basic Item class: Code:
class item
{
public:
item(char *name=NULL, int p=0, int uses=1, int m1=0, int m2=0, int m3=0, int m4=0, int m5=0, int m6=0, int m7=0, int m8=0);
item(string name=NULL, int p=0, int uses=1, int m1=0, int m2=0, int m3=0, int m4=0, int m5=0, int m6=0, int m7=0, int m8=0);
virtual ~item();
int uses() { return myuses; };
long int price() { return myprice; };
virtual bool use(Player *p) = 0;
virtual void undo() = 0;
bool operator<(const item &right) const;
bool operator>(const item &right) const;
bool operator==(const item &right) const;
bool operator!=(const item &right) const;
protected:
string myname; // Name to use in game & sorting
long int myprice; // How much does this item cost to buy (half to sell)
int mymodifiers[8]; // Up to 8 different modifiers handled in the sub-classes
int myuses; // How many times you can use this item
};
and what I need to do is have the player class contain a list of items (their inventory). However, in order to declare the item class or any sub-classes of that into the Player class, I need to #include "item.h" in the player.h file. Also, I need to #include "player.h" in the item.h file if I'm going to use a function that has a pointer to a Player object as a parameter. I have done this before with the aClient and Player classes, because an aClient has a player, and a Player also has a function that takes an aClient pointer as its parameter. My only guess is that the includes, now that they are three layers deep (player.h includes item.h and vice versa, and player.h includes aClient.h and vice versa), things are getting too hairy and are out of ISO C++ standards. I get these errors when I compile as soon as I put the #include "item.h" in the player.h file: Code:
[gameserv@titan gameserv]$ make
g++ -Wall -g -O2 -c aClient.cpp
In file included from pouch.h:4,
from player.h:7,
from aClient.h:6,
from aClient.cpp:1:
item.h:12: error: expected `)' before "name"
item.h:18: error: `Player' has not been declared
item.h:18: error: ISO C++ forbids declaration of `p' with no type
item.h:27: error: `string' does not name a type
item.h:36: error: expected `)' before "name"
item.h:39: error: `Player' has not been declared
item.h:39: error: ISO C++ forbids declaration of `p' with no type
item.h:40: error: `Player' has not been declared
item.h:40: error: ISO C++ forbids declaration of `p' with no type
item.h:47: error: expected `)' before "name"
item.h:50: error: `Player' has not been declared
item.h:50: error: ISO C++ forbids declaration of `p' with no type
item.h:51: error: `Player' has not been declared
item.h:51: error: ISO C++ forbids declaration of `p' with no type
make: *** [aClient.o] Error 1
Now, I'm not sure how much of the object hierarchy you need to understand to help me, but you can surely see I'm in a bit of a pickle here... I can't think of any other methods that would allow me to use polymorphism to set a player's attributes and still allow mostly modular item coding if they can't access the specific player that is using it... the only way I can think to do that is to put a pointer to the player in the use() function for an item... meaning I want to use the item on the player I put into the function. If anyone has any ideas, concerns, or comments, please do post ![]() |
|
#2
|
|||
|
|||
|
The Player class
Also, the player class is this:
Code:
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include "aClient.h"
#include "pouch.h"
using namespace std;
typedef struct monster_ Monster;
class aClient; // forward declaration
class Player {
public:
Player();
Player(aClient *);
Player(char *);
Player(string);
~Player();
void setData(Player *);
void setPassword(const char *p);
void reset();
long int getFlags() { return flags; }; // Returns the Client's current flags
// Functions also return the flags after modifying them
long int setFlags(long int); // Sets the clients flags to a new value
long int addFlag(long int); // Adds a flag to the client's flags
long int remFlag(long int); // Removes a flag from the client's current flags
//weapon *getWeapon() { return w; };
//armor *getArmor() { return a; };
//void setWeapon (weapon &); // Set a player's weapon to some item
//void setArmor (armor &); // Set a player's weapon to some item
int weapon;
int armor;
string name; // Player's Name
int level; // Player's level (1-12)
long int exp; // Player's experience
long int gold; // Gold on hand
long int bank; // Gold in the bank
int hp; // Current Hit Points (health)
int maxhp; // Maximum Hit Points
int strength; // Player's Strength
int defense; // Player's defensive strength
int forest_fights; // Amount of forest fights left today
int player_fights; // Amount of player<->player fights for today
string password; // Player's encrypted password
Pouch inventory; // This contains their potions, etc.
long int lastcommand; // timestamp for the last command typed
long int lastlogin; // timestamp for the last login
aClient *client; // Pointer to the aClient this player is from
Monster *fight; // Pointer to the monster the player is currently fighting
Monster *master; // Pointer to the master the player is currently fighting
aClient *battle; // Pointer to the player this player is currently fighting
private:
long int flags; // Player's current flags
// weapon *w; // Player's weapon
//armor *a; // Player's armor
};
So you can see I want to have the class contain some armor and weapon objects which are sub-classes of item, but I can't include the item.h file without errors occuring because it includes the same file I'm including it from... however I have done this before and it works fine with other files, though one of the files is the same. |
|
#3
|
|||
|
|||
|
Just letting everyone know, I fixed this by adding a forward declaration:
class item; in the player.h The problem was because item.h included player.h, but player.h required things from item.h to function properly, so it just needed a declaration of the class. The implementation comes later, but it needed to know that the object existed. Tough to explain, but I'm up and running now, so disregard this post! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Inheritance, Polymorphism, and Composition |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|