Thread: Problems grasping .h files...

  1. #1
    Registered User FoFGhost's Avatar
    Join Date
    Jun 2011
    Location
    Iowa
    Posts
    10

    Unhappy Problems grasping .h files...

    Decided to make a RPG Adventure game using Allegro and C++ and I want to split my files up. Here's what I would want them to do.

    main.cpp
    Code:
    #include "player_class.cpp"
    #include <allegro.h>
    
    int main(){
      allegro_init();
      return 0;}
    END_OF_MAIN();
    player_class.cpp
    Code:
    #include "player_stats.hpp"
    
    int class_setup(int player_class){
      player_class = 1;
      return player_class;}
    
    player_class = class_setup(player_class);
    player_stats.hpp
    Code:
    int player_class = 0;
    That's the jist of what I want, but I get errors when compiling it. I'm just wondering why that is and how to fix it?
    Last edited by FoFGhost; 06-21-2011 at 09:40 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are the error messages?

    Why is player_class is a global variable?

    Why do you include a source file in another source file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yeah, you're doing a large number of things wrong. Here's the right way to do it:

    Code:
    //main.cpp
    #include "player.h"
    #include <allegro.h>
    #include <iostream>
    
    int main()
    {
      allegro_init();
      Player protagonist(1); //the main player 
      protagonist.setClass(2);//change the class
      protagonist.getClass(); //access the class
      return 0;
    }
    END_OF_MAIN();
    Code:
    //player.cpp
    #include "player.h"
    
    Player::Player(int pClass): pClass(pClass){
    }
    
    void Player::setClass(int pClass){
        this->pClass = pClass;
    }
    int Player::getClass() const{
      return pClass;
    }
    Code:
    //player.h
    #ifndef PLAYER_H
    #define PLAYER_H
    
    class Player{
      private:
        int pClass;
      public:
        Player(int pClass);
        void setClass(int pClass);
        int getClass() const;
    };
    
    #endif
    Better if you can understand this than jimmy your code to compile. I'm doing a lot of things differently, but each for a reason.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User FoFGhost's Avatar
    Join Date
    Jun 2011
    Location
    Iowa
    Posts
    10

    Red face

    Quote Originally Posted by King Mir View Post
    Yeah, you're doing a large number of things wrong. Here's the right way to do it:

    Code:
    //main.cpp
    #include "player.h"
    #include <allegro.h>
    #include <iostream>
    
    int main()
    {
      allegro_init();
      Player protagonist(1); //the main player 
      protagonist.setClass(2);//change the class
      protagonist.getClass(); //access the class
      return 0;
    }
    END_OF_MAIN();
    Code:
    //player.cpp
    #include "player.h"
    
    Player::Player(int pClass): pClass(pClass){
    }
    
    void Player::setClass(int pClass){
        this->pClass = pClass;
    }
    int Player::getClass() const{
      return pClass;
    }
    Code:
    //player.h
    #ifndef PLAYER_H
    #define PLAYER_H
    
    class Player{
      private:
        int pClass;
      public:
        Player(int pClass);
        void setClass(int pClass);
        int getClass() const;
    };
    
    #endif
    Better if you can understand this than jimmy your code to compile. I'm doing a lot of things differently, but each for a reason.
    I think I get it now. We're setting things up in "classes" and that's why I'm getting weird errors with the constructors and deconstructurers.

    I have one more question. What's the point of ifndef?

    Thanks for the advice by the way! ;]

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by FoFGhost View Post
    I think I get it now. We're setting things up in "classes" and that's why I'm getting weird errors with the constructors and deconstructurers.

    I have one more question. What's the point of ifndef?

    Thanks for the advice by the way! ;]
    Glad it's clear, cause I was expecting to do a lot of explaining.

    The include gaurds as they are called make it so that you can include the same header file more than once in the same translation unit. Header files can #include classes that the depend on, and source file can include all the headers that they depend on. This can cause the same file to be included twice. Include gaurds prevent this from being a problem.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User FoFGhost's Avatar
    Join Date
    Jun 2011
    Location
    Iowa
    Posts
    10
    Never mind! Fixed a simple typo! Ha ha.

    I forgot to include my header file in main instead of the .cpp file! ;D
    Last edited by FoFGhost; 06-21-2011 at 11:29 PM. Reason: Typo!

  7. #7
    Registered User FoFGhost's Avatar
    Join Date
    Jun 2011
    Location
    Iowa
    Posts
    10
    I changed your code you gave me a little bit and it still runs great, for now. Lol.

    Code:
    //main.cpp
    #include "player_stats.hpp"
    #include <iostream>
    #include <allegro.h>
    
    int main(){
      allegro_init();
    
      Player protagonist;
      protagonist.setClass(0);
      
      Player player;
      player.setClass(1);
    
      std::cout << protagonist.getClass() << "\n\n";
      std::cout << player.getClass() << "\n\n";
      std::cin.get();
    
      return 0;}
    END_OF_MAIN();
    Code:
    //player_class.cpp
    #include "player_stats.hpp"
      
    void Player::setClass(int pClass){
      this -> pClass = pClass;}
    
    int Player::getClass() const{
      return pClass;}
    Code:
    //player_stats.hpp
    //#ifndef PLAYER_H
    //#define PLAYER_H
    
    class Player{
      private:
        int pClass;
      public:
        void setClass(int pClass);
        int getClass() const;};
    
    //#endif
    I commented those out so I can think more on taking them out. Tell me what you think though! ;]
    Last edited by FoFGhost; 06-21-2011 at 11:57 PM. Reason: Typo.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int Player::getClass() const{
      return pClass;}
    Don't do that. Many people would argue this is bad style.
    Put it on a new line instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not grasping Booleans
    By esmelogo in forum C Programming
    Replies: 2
    Last Post: 11-19-2010, 04:03 AM
  2. Problems with saving files
    By akira181 in forum C++ Programming
    Replies: 5
    Last Post: 05-14-2006, 10:24 AM
  3. Problems using two files
    By dwygal in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2006, 09:15 PM
  4. Problems with Enum and Files
    By Robert_Sitter in forum Windows Programming
    Replies: 7
    Last Post: 11-25-2005, 04:31 PM
  5. Having problems using multiple files
    By crazyeyesz28 in forum C++ Programming
    Replies: 6
    Last Post: 03-22-2005, 06:40 PM

Tags for this Thread