Thread: include problem

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    include problem

    I need to include a .h file that has already been included in another file. Because I cant include the same .h files in two different classes, I am stuck.

    The following is an example of what I mean:

    Code:
    #include "a.h"
    
    int main()
    {
    	AClass aObj;
    	aObj.someFnct();
    	return 0;
    }
    The a.h file


    Code:
    #include "b.h"
    #include "x.h"
    
    class AClass
    {
    	...
    	...
    	BClass bObj;
    	XClass xObj;
    	void someFnct (); 
    };
    
    //-------------------------
    void AClass::someFnct ()
    {
    	bObj.foo();
    	...
    	...
    	xObj.problemFnct (bObj);
    }
    The b.h file


    Code:
    const int MAX = 50;
    
    class BClass
    {
    	int num;
    	char str[MAX];
    	...
    	...
    	void foo();
    };
    
    //-------------------------
    void BClass::foo ()
    {
    	AClass aObj; <-- ???
    	...
    	...
    }
    The x.h file


    Code:
    template <class T>
    class XClass
    {
    	...
    	...
    	void problemFnct(T &data);
    };
    
    //-------------------------
    template <class T>
    void XClass::problemFnct(T &data);
    {
    	AClass aObj;  <-- ???
    	...
    	...
    
    	aObj.someFnct();  <-- ???
    }
    How can I call a function or create an object that belongs to AClass from classes B and X
    simple is always an understatement.....

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Only solution I can think of right now is to merge the header files together. You should also remove the function definitions from the header file and put them in seperate .cpp files, for example:
    Code:
    //b.cpp
    #include "abx.h" //merged headers
    //-------------------------
    void BClass::foo ()
    {
    	AClass aObj; <-- ???
    	...
    	...
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    The actual files are separated..but I just kept it as one for example purposes. As for the merged headers...what is a merged header??
    simple is always an understatement.....

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Sorry, I should've been clearer. I just meant to put all the class declarations in one header file:
    Code:
    //abx.h
    class AClass
    {
    	...
    	...
    	BClass bObj;
    	XClass xObj;
    	void someFnct (); 
    };
    
    const int MAX = 50;
    
    class BClass
    {
    	int num;
    	char str[MAX];
    	...
    	...
    	void foo();
    };
    
    template <class T>
    class XClass
    {
    	...
    	...
    	void problemFnct(T &data);
    };
    And then of course include that file in the .cpp files
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Another, slightly more complicated solution is to use the Pimpl idiom (a.k.a. compilation firewall). The idea is that in your header file, you don't have any data members except one. That member is a pointer to a struct that is not defined in the header file. Since you can have a pointer to an undefined class or struct, you can define that struct in the cpp file.
    Code:
    // MyClass.h
    class MyClass
    {
    // ... Public Interface
    private:
        struct Impl;
        Impl* pimpl;
    };
    
    // MyClass.cpp
    
    #include "MyClass.h"
    // All your other header includes
    
    struct MyClass::Impl
    {
      // Data members
    };
    
    MyClass::MyClass() : pimpl(new Impl) { }
    MyClass::~MyClass() { delete pimpl; }
    Then in the rest of your code, you refer to data members as pimpl->datamembername. Anyway, you should probably read up on it for more information than this, but that is the basic idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  2. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:23 AM
  3. include problem
    By Strait in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2005, 04:01 PM
  4. Read and write hanging
    By zee in forum C Programming
    Replies: 8
    Last Post: 08-03-2004, 11:19 PM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM