Thread: Can you define a class and define all its member functions in one header file?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Question Can you define a class and define all its member functions in one header file?

    I was wondering if you can do this? I would like to make a new class to encapsulate some of my functions that are beginning to clutter my project like stray cats.

    I was wondering do I have to include the complete definitions in a .cpp file or can the whole lot go in a header file?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can put the whole lot in a header file.

    If it's a regular class (not a bunch of templates), it's more usual to separate them out.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    // This is myclass.h
    #ifndef MYCLASS_INCLUDED_H
    #define MYCLASS_INCLUDED_H
    class myClass {
        private:
            int foo;
        public:
            myClass() : foo(42) {}  // an inline implementation
            int getFoo();           // an implemetation elsewhere
    };
    #endif //MYCLASS_INCLUDED_H
    
    // This is myclass.cpp
    #include "myclass.h"
    int myClass::getFoo() {
        return foo;
    }
    
    // This is whoever needs to use myClass
    #include "myclass.h"
    int main ( ) {
        myClass obj;
        std::cout << "Answer=" << obj.getFoo() << "\n";
    }
    It doesn't have to be a class.
    Any group of similar functions could be separated out into a header file and an implementation file.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Thanks very much again Salem. Totally answered my question thanks

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Post Success with some observations

    It all worked (eventually). Some points to note though for anyone else who may ever read this thread (none of them I assume are anything other than typical C++ requirements).

    First mistake, in a class named D3DObj I forgot to add D3DObj:: before the function definitions. This threw all sorts of errors. So for example this:

    Code:
    void InitDirectX (parameters)
    failed whereas:

    Code:
    void D3DObj::InitDirectX (parameters)
    was ok. For some bizarre reason I kept on getting compiler errors stating the function declaration in the class and the function definition below it did not match. This was even after they did, although my machine was running very slow at the time so after a while the error would clear. That was a bit of a strange one.

    Also where I forgot to define constructor and destructor functions but left their declarations in the class I actually managed to get a linker error. Seems the compiler was happy to allow it to pass hoping they were defined elsewhere, when they were not. Was confounded at first as to how I got a linker error from a header file which just had a class declaration in it.

    Only last bit of trouble I had was when I passed a ComPtr into a function assuming for ComPtr's I did not need to pass by reference for some reason. Well I did and the debugger showed me some nullptr errors as a result of passing copies rather than by reference.

    All works ok now and the whole thing sits in one header file

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. define functions in another file
    By suryak in forum C Programming
    Replies: 14
    Last Post: 08-09-2011, 12:59 AM
  2. how to define a static member in a template class
    By wanziforever in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2009, 04:44 AM
  3. Can you define a static member function in a class?
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2008, 09:23 PM
  4. Define Header File
    By HLMetroid in forum C Programming
    Replies: 2
    Last Post: 10-04-2007, 02:22 PM
  5. #define Header files. End Of File Errors.
    By bartybasher in forum C Programming
    Replies: 8
    Last Post: 07-28-2003, 03:03 PM

Tags for this Thread