Thread: [HOWTO] write classes headers

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    [HOWTO] write classes headers

    Hello!

    I'm a newbye with C++ and I come from Java;
    I've more or less found out how to use different cpp files using .h files, then I wrote a class over my main() method.
    What I would like to do now is to put the class in an another file, like Class.cpp
    and make its header. The problem is when I build the main program: it states "undefined reference to <class>::<method>"
    I think I'm making some mistake writing the h file. Which is the correct way?

    If necessary, I can post the code, it's very short.
    Thanks, bye

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Header file:
    Code:
    #ifndef SOME_RANDOM_STRING_IDENTIFIER
    #define SOME_RANDOM_STRING_IDENTIFIER
    
    #include <whatever libs you need>
    
    class my_class {
    private: 
      // ...
    
    public:
      // ...
    };
    
    #endif
    cpp file:
    Code:
    #include "the above header file, same dir"
    
    my_class::my_methods( my_parameters ) {
      my_body;
    }
    
    etc
    main:
    Code:
    #include "first header file"
    // ... 
    
    int main() {
      my_class my_instance;
      // ...
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    I think I made the same thing, but nothing changed

    Here's the exact error:
    .objs\main.o:main.cpp.text+0x8a): undefined reference to `MyClasse::Mymethod()'

    I omitted this:
    "#ifndef SOME_RANDOM_STRING_IDENTIFIER
    #define SOME_RANDOM_STRING_IDENTIFIER"
    I've seen it around but I believed it's not obligatory... maybe I was wrong? Must I put it in?

    Thanks again

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    No. You don't have to put it in. Its purpose is to protect against multiple files including the same file again and again when it only needs to be included once. Suppose you have another file and x.h is included in that. Then in your main file you include x.h. It'll error, probably, saying there are multiple definitions of whatever's in the file. The #ifndef preprocessor control statements protect against this. The "SOME_RANDOM_STRING_IDENTIFIER" I generally relate to the class I'm using. So it could be: "TWOMERS_MY_CLASS", for the above example.

    Post some code. I'm curious now.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    CPP FILE:
    Code:
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "grafiche.h"
    
    class Schermo {
        SDL_Surface *sfondo, *foo;
      public:
        SDL_Surface *getSfondo() { return sfondo;}
        SDL_Surface *getFoo(){ return foo;}
        void initGfx();
        void pulisci(){
            SDL_FreeSurface( foo ); SDL_FreeSurface( sfondo );
        }
    
      };
    
    void Schermo::initGfx() {
        sfondo = load_image( "prato.png" );
        foo = load_image( "casa.png" );
    }

    HEADER FILE

    Code:
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "grafiche.h"
    
    class Schermo {
        SDL_Surface *sfondo, *foo;
      public:
        SDL_Surface *getSfondo();
        SDL_Surface *getFoo();
        void initGfx();
        void pulisci();
      };

    Thanks!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are re-declaring the class inside the cpp file. Don't do that, instead put the declaration in the header and #include that header. You're allowed to implement those member functions inside the class declarations in the header, so update your header file with the class definition from the cpp file.

    Also, you pretty much do have to put in header include guards. Technically there are workarounds that make it not required, and some small programs won't need it, but most projects will fail to compile or link if you don't add them.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    There is a FAQ on writing multi-file projects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Quote Originally Posted by gibbofresco View Post
    CPP FILE:

    Code:
    #include "headerfilenamefrombelow"
    
    void Schermo::initGfx()
    {
        sfondo = load_image( "prato.png" );
        foo = load_image( "casa.png" );
    }
    
    void Schermo::pulisci()
    {
        SDL_FreeSurface( foo );
        SDL_FreeSurface( sfondo );
    }

    HEADER FILE

    Code:
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "grafiche.h"
    
    class Schermo
    {
        SDL_Surface *sfondo, *foo;
    
        public:
        SDL_Surface *getSfondo();
        SDL_Surface *getFoo();
    
        void initGfx();
        void pulisci();
    };

    Thanks!
    I think that's more along the lines of what you want.
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I think that's more along the lines of what you want.
    Almost. The header file should include the inline function definitions that were in the original source file.

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    And for safety reasons it's always good to include header guards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. Why just use prototypes with classes?
    By jsrig88 in forum C++ Programming
    Replies: 5
    Last Post: 08-11-2006, 06:52 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Redefinition of classes (#include headers)
    By cjschw in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2003, 09:24 PM