Header Files and Classes.

This is a discussion on Header Files and Classes. within the C++ Programming forums, part of the General Programming Boards category; I've been frustrated at this particular problem for about six hours now, and have searched the C++ board to no ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

    Header Files and Classes.

    I've been frustrated at this particular problem for about six hours now, and have searched the C++ board to no avail. I've also checked every C++ book I have, and nothing can help me find the answer to this problem.

    I have a .cpp file in which I declare a class "Player play;". This holds all the variables for my player character. Now, I have a header file that I would like to hold all the information about my fighting functions and variables. However, I can't redefine the class ("Player player;" for example), because that makes a new instance of the class and doesn't copy over all the variables. My original instance of the class can't be accessed by this file, and I can't #include main.cpp because I get recursion errors (and header guards don't work).,

    So the question is: How do I copy over an instance of a class from a .cpp file to a .h file to run functions?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Well first of try to keep actully definations out of header files they are meant for prototyping not defination.
    You'll want to do something like this
    Code:
    //class.h
    #infdef CLASS_H
    #define CLASS_H
    class loser
    {
      public:
       void setLoserPower(const int loserPowerSet);
      private:
        int loserPower;
    };
    #endif// CLASS_H
    Code:
    //class.cpp
    #include "class.h"
    void loser::setLoserPower(int loserPowerSet)
    {
      loserPower=loserPowerSet;
    }
    Code:
    //main.cpp
    #include "class.h"
    
    int main()
    {
      loser brian;
      brian.setLoserPower(100);
      return 0;
    }
    And to answer your question pass it in a function
    Hope I din't confuse you
    Woop?

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Fine heres a whole example
    Code:
    //main.cpp
    #include <iostream>
    #include "class.h"
    #include "function.h"
    using namespace std;
    
    int main()
    {
      loser brian;
      fight(&brian);
      cout<<brian.getLoserPower();
      cin.get();
      return 0;
    }
    Code:
    //class.h
    #ifndef CLASS_H
    #define CLASS_H
    class loser
    {
      public:
       void setLoserPower(const int loserPowerSet);
       int getLoserPower();
      private:
        int loserPower;
    };
    #endif// CLASS_H
    Code:
    //function.h
    #ifndef FUNCTION_H
    #define FUNCTION_H
    #include "class.h"
    
    void fight(loser *name);
    
    #endif//FUNCTION_H
    Code:
    //class.cpp
    #include "class.h"
    void loser::setLoserPower(int loserPowerSet)
    {
      loserPower=loserPowerSet;
    }
    int loser::getLoserPower()
    {
      return loserPower;
    }
    Code:
    //function.cpp
    #include "function.h"
    
    void fight(loser *name)
    {
      name->setLoserPower(100);
    }
    Woop?

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Ok, I've been trying what you told me to try, and I've hit a brick wall when it comes to defining classes in a header file. Maybe see what you can make of it?

    Code:
    #ifndef MAIN_H
    #define MAIN_H
    
    // Includes
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <ctime>
    
    #include "player.h"
    #include "enemy.h"
    #include "fight.h"
    #include "load.h"
    
    // Functions
    
    void Autoexec();
    void InputLoop();
    void Action(char act[16]);
    void Attack(char* efname);
    
    void LoadRoom(char* filename);
    void LoadEnemy();
    
    void FAction(char act[128]);
    void FInputLoop();
    
    // Variables
    
    char north[16], south[16], east[16], west[16], name[64], efname[64];
    char filename[256] = "start.txt";
    bool n, s, e, w, dead, errflag, winflag, quitflag;
    
    Player play;
    Enemy enemy;
    
    #endif
    Errors:

    Code:
    --------------------Configuration: Texted Sun - Win32 Debug--------------------
    Compiling...
    player.cpp
    c:\program files\microsoft visual studio\myprojects\texted sun\main.h(35) : error C2146: syntax error : missing ';' before identifier 'play'
    c:\program files\microsoft visual studio\myprojects\texted sun\main.h(35) : error C2501: 'Player' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\texted sun\main.h(35) : fatal error C1004: unexpected end of file found
    enemy.cpp
    c:\program files\microsoft visual studio\myprojects\texted sun\main.h(36) : error C2146: syntax error : missing ';' before identifier 'enemy'
    c:\program files\microsoft visual studio\myprojects\texted sun\main.h(36) : error C2501: 'Enemy' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\texted sun\main.h(36) : fatal error C1004: unexpected end of file found
    main.cpp
    load.cpp
    fight.cpp
    Error executing cl.exe.
    
    Texted Sun.exe - 6 error(s), 0 warning(s)

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You don't need to define classes in your header read my above post you need to define a function that takes a pointer to a class for an argument
    Woop?

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by prog-bman
    You don't need to define classes in your header read my above post you need to define a function that takes a pointer to a class for an argument
    A pointer to a class. You have /completely/ lost me. You can't make a pointer to a specific instance of a class, as far as I thought?

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    ok say i have this in my main cpp file
    Code:
    #include <iostream>
    using namespace std;
    class brian
    {
      public:
        int height;
    };
    void functionToSetBrianHeight(brian *setHeigth);
    
    int main()
    {
      brian me;
      functionToSetBrianHeight(&me);
      cout<<me.height;
    }
    
    void functionToSetBrianHeight(brian *setHeight)
    {
      setHeight->height =10;
    }
    Even though your decalring a setHeight brian when you pass it, setHeight is replaced by whatever you input there
    Woop?

  8. #8
    nbk
    nbk is offline
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    Code:
    #include <iostream>
    using namespace std;
    class brian
    {
      public:
        int height;
    };
    void functionToSetBrianHeight(brian &);
    
    int main()
    {
      brian me;
      functionToSetBrianHeight(me);
      cout<<me.height;
      system("PAUSE");
    }
    
    void functionToSetBrianHeight(brian &setHeight) //Making a reference to whatever was passed
    {
      setHeight.height=5;
    }
    Had to add something bman

    This is passing by reference(you may or may not have known), just adding.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>'Player' : missing storage-class or type specifiers
    Let's see player.h and enemy.h. The problem is the class isn't even declared at the point where you declare a Player called player. Since you've included the header for Player (I'm assuming that's what player.h is), that isn't the problem; you'll need to show us what's inside the header then.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Both Player.h and Enemy.h look like this. Just change the word "Player" to "Enemy". I could probally do this with one class and two instances, but that's a different issue.

    Here ya go.

    Code:
    class Player
    {
    	public:
    		Player();
    		~Player();
    		int SetStat(char* stat, int level);
    		int DispStat(char* stat);
    		int GetStat(char* stat);
    		void DAStats();
    
    	private:
    		int HP, MaxHP, MP, MaxMP, AGI, CHR, CON, INT, STR, WIS, GP, XP, LVL;
    };
    
    Player::Player() {}
    
    Player::~Player() {}
    
    int Player::SetStat(char* stat, int level)
    {
    	if (strcmp(stat, "HP") == 0)
    	{
    		HP = level;
    	}
    
    	if (strcmp(stat, "MaxHP") == 0)
    	{
    		MaxHP = level;
    		HP = MaxHP;
    	}
    
    	if (strcmp(stat, "MP") == 0)
    	{
    		MP = level;
    	}
    
    	if (strcmp(stat, "MaxMP") == 0)
    	{
    		MaxMP = level;
    		MP = MaxMP;
    	}
    
    	if (strcmp(stat, "AGI") == 0)
    	{
    		AGI = level;
    	}
    
    	if (strcmp(stat, "CHR") == 0)
    	{
    		CHR = level;
    	}
    
    	if (strcmp(stat, "CON") == 0)
    	{
    		CON = level;
    	}
    
    	if (strcmp(stat, "INT") == 0)
    	{
    		INT = level;
    	}
    
    	if (strcmp(stat, "STR") == 0)
    	{
    		STR = level;
    	}
    
    	if (strcmp(stat, "WIS") == 0)
    	{
    		WIS = level;
    	}
    
    	if (strcmp(stat, "GP") == 0)
    	{
    		GP = level;
    	}
    
    	if (strcmp(stat, "XP") == 0)
    	{
    		XP = level;
    	}
    
    	if (strcmp(stat, "LVL") == 0)
    	{
    		LVL = level;
    	}
    
    	return(0);
    }
    
    int Player::DispStat(char* stat)
    {
    	if (strcmp(stat, "HP") == 0)
    	{
    		std::cout << HP;
    	}
    
    	if (strcmp(stat, "MaxHP") == 0)
    	{
    		std::cout << MaxHP;
    	}
    
    	if (strcmp(stat, "MP") == 0)
    	{
    		std::cout << MP;
    	}
    
    	if (strcmp(stat, "MaxMP") == 0)
    	{
    		std::cout << MaxMP;
    	}
    
    	if (strcmp(stat, "AGI") == 0)
    	{
    		std::cout << AGI;
    	}
    
    	if (strcmp(stat, "CHR") == 0)
    	{
    		std::cout << CHR;
    	}
    
    	if (strcmp(stat, "CON") == 0)
    	{
    		std::cout << CON;
    	}
    
    	if (strcmp(stat, "INT") == 0)
    	{
    		std::cout << INT;
    	}
    
    	if (strcmp(stat, "STR") == 0)
    	{
    		std::cout << STR;
    	}
    
    	if (strcmp(stat, "WIS") == 0)
    	{
    		std::cout << WIS;
    	}
    
    	if (strcmp(stat, "GP") == 0)
    	{
    		std::cout << GP;
    	}
    
    	if (strcmp(stat, "XP") == 0)
    	{
    		std::cout << XP;
    	}
    
    	if (strcmp(stat, "LVL") == 0)
    	{
    		std::cout << LVL;
    	}
    
    	return(0);
    }
    
    int Player::GetStat(char* stat)
    {
    	if (strcmp(stat, "HP") == 0)
    	{
    		return(HP);
    	}
    
    	if (strcmp(stat, "MaxHP") == 0)
    	{
    		return(MaxHP);
    	}
    
    	if (strcmp(stat, "MP") == 0)
    	{
    		return(MP);
    	}
    
    	if (strcmp(stat, "MaxMP") == 0)
    	{
    		return(MaxMP);
    	}
    
    	if (strcmp(stat, "AGI") == 0)
    	{
    		return(AGI);
    	}
    
    	if (strcmp(stat, "CHR") == 0)
    	{
    		return(CHR);
    	}
    
    	if (strcmp(stat, "CON") == 0)
    	{
    		return(CON);
    	}
    
    	if (strcmp(stat, "INT") == 0)
    	{
    		return(INT);
    	}
    
    	if (strcmp(stat, "STR") == 0)
    	{
    		return(STR);
    	}
    
    	if (strcmp(stat, "WIS") == 0)
    	{
    		return(WIS);
    	}
    
    	if (strcmp(stat, "GP") == 0)
    	{
    		return(GP);
    	}
    
    	if (strcmp(stat, "XP") == 0)
    	{
    		return(XP);
    	}
    
    	if (strcmp(stat, "LVL") == 0)
    	{
    		return(LVL);
    	}
    
    	return(-1);
    }
    
    void Player::DAStats()
    {
    	std::cout << "HP: " << HP << " / " << MaxHP << "\n";
    	std::cout << "MP: " << MP << " / " << MaxMP << "\n";
    	std::cout << "AGI: " << AGI << "\n";
    	std::cout << "CHR: " << CHR << "\n";
    	std::cout << "CON: " << CON << "\n";
    	std::cout << "INT: " << INT << "\n";
    	std::cout << "STR: " << STR << "\n";
    	std::cout << "WIS: " << WIS << "\n";
    	std::cout << "GP: " << GP << "\n";
    	std::cout << "XP: " << XP << "\n";
    	std::cout << "LVL: " << LVL;
    }

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oops, wait a minute.. the errors are in Player.cpp and Enemy.cpp I think I understand your problem now (reading alllll the way back to the first post lol). You're saying you defined the variables in main.h, but it's not recognized in Player.cpp and Enemy.cpp, and you want to know how to fix that, right? The fix is pretty easy actually. In Player.cpp, at the top of the file put "extern Player play;" and in Enemy.cpp put "extern Enemy enemy;". That tells the compiler that play and enemy have been defined in another file somewhere, and it's ok to use it in those .cpp files.

    Also, in case you haven't done so already, you'll most likely need to #include "Player.h" into player.cpp and same for enemy (the code for the classes should be in the .cpp and the declaration of the class should be in the .h).

    Hope this helps!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 06:12 PM
  4. 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
  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