ok guys and gals here's my delema, i have this code:

all it basically does is define two classes, one that defines the stuff for the player and his actions, the other for the enemies (or monsters)

the problem is that i have funtions in both that require i include the other, because i am programming in c++ i understand that code only goes in a linear direction, down....... so when the compiler tries to compile is says that "character is not defined" because it hasn'e been defined yet but is being passed as an argument in monster...........is there any way that i can do this??? i can't think of anything myself ...............................

(sorry word wrap on makes sloppy looking code it doesn't really go on to the next line like that...........

class monster{
	
		public:
			monster(){}
			void initiate_enemy(int level,lvl_type ctype, int player_x, int player_y,char [50][50]);
			~monster(void);
			void get_pos(int & xpos, int & ypos);
			void truepos(int & xpos, int & ypos);
			bool move(char [50][50]);
			void take_damage(int dam, int pierc);
			void cast_spell();
			void melee_attack(character player);
			void get_stats(int & health, int & mana, int & dam, int & defence, int & pierc);
			void show_stats();
			bool passable(char map[50][50],direction direction);

			direction facing;
			bool dead;
		

		private:

			//basic character stats
			string name;
			lvl_type type;
			int hp;
			int max_hp;
			int lvl;
			int mp;
			int max_mp;
			int gp;
			int damage;
			int def;
			int pierce;

			//basic elements the stats are composed of
			int agility;
			int health_points;
			int str;
			int intelect;

			//for the direction facing, the position, and the map itself
			int x_pos;
			int y_pos;
			int x;
			int y;
			int movedup,moveddown,movedright,movedleft;
			bool possible_move;


			//needed for enemies
			bool possible_attack;

	};

class character{
	
		public:
			character(){}
			void initiate_player();
			~character(void);
			void get_pos(int & xpos, int & ypos);
			void truepos(int & xpos, int & ypos);
			bool move(direction direction,char [50][50]);
			void take_damage(int dam, int pierc);
			void cast_spell();
			void melee_attack(monster enemy[50]);
			void get_stats(int & health, int & mana, int & dam, int & defence, int & pierc);
			void show_stats();
			void add_page(pages type);
			bool passable(char map[50][50],direction direction);

			direction facing;
			bool dead;
		

		private:

			//basic character stats
			string name;
			lvl_type type;
			int hp;
			int max_hp;
			int lvl;
			int mp;
			int max_mp;
			int gp;
			int damage;
			int def;
			int pierce;

			//basic elements the stats are composed of
			int agility;
			int health_points;
			int str;
			int intelect;

			//to keep track of the items
			int redpotions;
			int bluepotions;
			int pages_earth;
			int pages_fire;
			int pages_wind;
			int pages_ice;

			//for the direction facing, the position, and the map itself
			int x_pos;
			int y_pos;
			int x;
			int y;
			int movedup,moveddown,movedright,movedleft;
			bool possible_move;


			//needed for enemies
			bool possible_attack;

	};

Recommended Answers

All 3 Replies

(sorry word wrap on makes sloppy looking code it doesn't really go on to the next line like that...........

Then stop TABbing so deeply. Use only 4 spaces, not 4 TABs

class monster{
    
    public:
        monster(){}
        void initiate_enemy(int level,lvl_type ctype, int player_x, int player_y,char [50][50]);
        ~monster(void);
        void get_pos(int & xpos, int & ypos);
        void truepos(int & xpos, int & ypos);
        bool move(char [50][50]);
        void take_damage(int dam, int pierc);
        void cast_spell();
        void melee_attack(character player);
        void get_stats(int & health, int & mana, int & dam, int & defence, int & pierc);
        void show_stats();
        bool passable(char map[50][50],direction direction);

        direction facing;
        bool dead;

Like that...

You need to tell the compiler that the class you are using exists. You do this by something known as forward declarations.

// Enemy.h
#ifndef ENEMY_H
#define ENEMY_H
class Player ;  // forward declaration

class Enemy
{
    int c ;
    int d ;

    public:
        int get (Player a) ;  // now this works

} ;
#endif
// Player.h
#ifndef PLAYER_H
#define PLAYER_H

class Enemy ; // forward declaration

class Player
{
    int a ;
    int b ;

    public :
        int get (Enemy a) ;  // now this works
} ;
#endif
// Driver.cpp

#include "a.h"
#include "b.h"

int main()
{
    Enemy e ;
    Player p ;

    return 0;
}

cool ty S.O.S helped a lot!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.