classes and header files

This is a discussion on classes and header files within the C++ Programming forums, part of the General Programming Boards category; Below you will see two header files and an implementation file(the one that has errors). I have included the error ...

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Exclamation classes and header files

    Below you will see two header files and an implementation file(the one that has errors). I have included the error messages I am recieving below the two files. I don't know what I am doing wrong, so if you have and questions, just ask.

    -also included the systemhlp.h file, just in case you wanna try and compile it; all the files are there for you.

    thanks.

    Code:
    //enemy.h
    
    #ifndef _enemy_h_
    #define _enemy_h_
    #include "player.h" 
    
    class Player;//forward declaration
    
    using namespace std;
    class Enemy
    {
        public:
            void stats();
            void bStats();
            int attack(Enemy& enemyN, Player* playerN);
            Enemy(int num);
        private:
            int m_hp;//=======================LINE 19======================
            int m_mp;
            int m_str;
            int m_exp;
            int m_level;
            string m_name;
    };
    #endif


    Code:
    //player.h
    
    #ifndef _player_h_
    #define _player_h_
    #include "enemy.h"
    
    class Enemy;//forward declaration
    
    using namespace std;
    
    class Player
    {
        public:
            Player(int gClass);
            void levelUp();
            bool canLevelUp();
            void stats();
            void bStats();
            int attack(Enemy* enemyN, Player& playerN);
            void inventory();
            int heal(Player playerN);
            //int mag(Player* playerN, Enemy* enemyN, string spell);
        private:
            int MAX_HP;
            int MAX_MP;
            int m_level;
            int m_hp;
            int m_str;
            int m_mp;
            int m_exp;
            int m_mPow;
            char m_name[12];//Player's name
    };
    #endif
    Code:
    //player.cpp
    
    #include<iostream>
    #include<string>
    #include <stdio.h>
    #include <stdlib.h>
    #include "systemhlp.h"
    #include "player.h"
    #include "enemy.h"
    
    using namespace std;
    
    Player::Player(int gClass)
    {
        if(gClass == 1)
        {
            m_level = 1;
            m_hp = 100;
            m_str = 20;
            m_mp = 50;
            MAX_HP = m_hp;
            MAX_MP = m_mp;
        }
        if(gClass == 2)
        {
            m_level = 1;
            m_hp = 80;
            m_str = 10;
            m_mp = 100;
            MAX_HP = m_hp;
            MAX_MP = m_mp;
        }
        if(gClass == 3)
        {
            m_level = 1;
            m_hp = 90;
            m_str = 15;
            m_mp = 75;
            MAX_HP = m_hp;
            MAX_MP = m_mp;
        }
    };
    
    bool Player::canLevelUp()
    {
        if(m_level == 1 && m_exp >= 32)
        {
            return true;
        }
        else if(m_level == 2 && m_exp >= 96)
        {
            return true;
        }
        else if(m_level == 3 && m_exp >= 208)
        {
            return true;
        }
        else if(m_level == 4 && m_exp >= 400)
        {
            return true;
        }
        else if(m_level == 5 && m_exp >= 672)
        {
            return true;
        }
        else if(m_level == 6 && m_exp >= 1056)
        {
            return true;
        }
        else if(m_level == 7 && m_exp >= 1552)
        {
            return true;
        }
        else if(m_level == 8 && m_exp >= 2184)
        {
            return true;
        }
        else if(m_level == 9 && m_exp >= 2976)
        {
            return true;
        }    
        else if(m_level == 10 && m_exp >= 3936)
        {
            return true;
        }    
        else if(m_level == 11 && m_exp >= 5080)
        {
            return true;
        }
        else if(m_level == 12 && m_exp >= 6432)
        {
            return true;
        }
        else if(m_level == 13 && m_exp >= 7992)
        {
            return true;
        }
        else if(m_level == 14 && m_exp >= 9784)
        {
            return true;
        }
        else if(m_level == 15 && m_exp >= 11840)
        {
            return true;
        }    
        else if(m_level == 16 && m_exp >= 14152)
        {
            return true;
        }    
        else if(m_level == 17 && m_exp >= 16736)
        {
            return true;
        }    
        else if(m_level == 18 && m_exp >= 19616)
        {
            return true;
        }    
        else if(m_level == 19 && m_exp >= 22832)
        {
            return true;
        }   
        else
        {
            return false;
        }
    }
    
    void Player::levelUp()
    {
        m_level++;
        if(m_level <= 10 && canLevelUp() == true)
        {
            m_hp += 50;
            m_str += 5;
            m_mp += 50;
            MAX_HP = m_hp;
            MAX_MP = m_mp;
        }
        if(m_level <= 15 && canLevelUp() == true)
        {
            m_hp += 45;
            m_str += 4;
            m_mp += 45;
            MAX_HP = m_hp;
            MAX_MP = m_mp;
        }
        if(m_level <= 20 && canLevelUp() == true)
        {
            m_hp += 35;
            m_str += 3;
            m_mp += 35;
            MAX_HP = m_hp;
            MAX_MP = m_mp;
        }
    }
    
          void Player::stats()
          {
               cout<<".~-Stats-~."<<endl;
               cout<<"Level: "<<m_level<<endl;
               cout<<m_name<<endl;
               cout<<"HP: "<<m_hp<<endl;
               cout<<"MP: "<<m_mp<<endl;
               cout<<"Strength: "<<m_str<<endl;
               cout<<"Exp: "<<m_exp<<endl;
               system("pause");
        }
    
        void Player::bStats()
        {
               cout<<m_name<<endl;
               cout<<"HP: "<<m_hp<<endl;
               cout<<"MP: "<<m_mp<<endl;
               cout<<"\n"<<endl;
        }
        int Player::heal(Player playerN)
          {
               if(playerN.m_mp >= 5)
               {
                     int x = ((playerN.m_level*playerN.MAX_MP)/128) + ((rand() % 6) + 4);
                     system("cls");
                     cout<<"You gain "<<x<<" points of health!\n"<<endl;
                     system("pause");
                     playerN.m_hp = playerN.m_hp + x;
                     playerN.m_mp = playerN.m_mp - 5;
                     if(playerN.m_hp > playerN.MAX_HP)
                     {
                            playerN.m_hp = playerN.MAX_HP;
                     }
                     return playerN.m_mp, playerN.m_hp;
               }
          }
    
    int Player::attack(Enemy* enemyN, Player& playerN)
    {
                cls();
                int x = 20 * (playerN.m_str/100) * 20;
                int criticalHit = x * (2/10);
                if(((rand() % 6) + 1) == 3)
                {
                    x += criticalHit;
                    cout<<"Critical Hit!"<<endl;
                }
    
               enemyN->m_hp = enemyN->m_hp - x;//=========LINE 304==============
               cout<<"You hit for "<<x<<" points of damage!\n"<<endl;
               pause();
               cls();
               if(enemyN->m_hp < 0)//==================LINE 308================
               {
                    enemyN->m_hp = 0;//=================LINE 310===============
               }
           return enemyN->m_hp;  //==================LINE 313==============
    }
    Code:
    Line         Message               File
    
    19            `int                  enemy.h
    304           within              player.cpp
    19            `int                  enemy.h
    304           within              player.cpp
    19             `int                 enemy.h
    308           within              player.cpp
    19             `int                 enemy.h
    310           within             player.cpp
    19             `int                 enemy.h
    313           within              player.cpp






    Code:
    //systemhelp.h
    #include <iostream>
    #include <stdlib.h>
    
    
    void pause()
    {
        std::cout<<"\n\n==>"<<std::endl;
        std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
    }
    
    void cls()
    {
        system("cls");
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In both header files, you only need the forward declarations, not the #includes.

  3. #3
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    Thanks for that little bit of information, lol, I didn't realize I put that in there. But, unfortuantely I still have all of the listed errors (thanks for the help though).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That's the entire text of your errors?

  5. #5
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    yeah, I copied it straight from the compiler.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Is something wrong with your compiler? That is not an error message. Maybe you need to expand the window or something?

    There might be another window that has the full text of the error message (like an output window). It's hard to work without actual error messages.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    BTW this would be much better than a large number of if statements:

    Code:
    bool Player::canLevelUp()
    {
        // XPToNextLevel[10], for example, is the XP needed to be able to level to 11.
        // First entry is 0 because we start at level 1.
        const static int XPToNextLevel[] = {0, 32, 96, 208, 400, 672, 1056, 1552, 2814, 2976, 3936,
                                            5080, 6432, 7992, 9784, 11840, 14152, 16736, 19616, 22832};
        
        return (m_xp >= XPToNextLevel[m_level] );
    }
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    yeah, I just found the compile log, which shows the FULL error. It said that m_hp was declared private in both instances and all I had to do was make it public.

    thanks.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Better design might be to keep it private and provide a set and/or get method for it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files and classes
    By disruptivetech in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2008, 09:02 AM
  2. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 03:55 AM
  3. Learning how to use classes and create header files
    By Welshy in forum C++ Programming
    Replies: 10
    Last Post: 04-19-2005, 12:33 PM
  4. Classes and header files
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 10:42 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21