Thread: problem with compiling

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    problem with compiling

    Hello..

    I have 2 headers.

    The first one is:

    Code:
    #ifndef SESSION_HEADER
    #define SESSION_HEADER
    
    #include "header2.h"
    class session {
    public:
    	session() { }
    	thread *threads;
    	thread *get_thread(unsigned short id);
    	..etc..
    };
    
    #endif
    And the other one is:

    Code:
    #ifndef THREAD_HEADER
    #define THREAD_HEADER
    
    #include "header1.h"
    
    class thread {
    public:
    	thread(session *se) { s = se; }
    
    	session *s;
    	thread *next;
    };
    
    #endif

    Now im getting errors when compiling, I think because the compiler wants to include header which would need the first header.. I know for extern, but is this the way how should I do it? And if I should, how would be the right way to do it?

    Thanks for help guys

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are using only pointers to the other class, so a forward declaration should do.
    e.g.
    Code:
    #ifndef SESSION_HEADER
    #define SESSION_HEADER
    
    // #include "header2.h" // not needed
    
    class thread;
    
    class session {
    public:
    	session() { }
    	thread *threads;
    	thread *get_thread(unsigned short id);
    	..etc..
    };
    
    #endif
    Kurt

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    One possibility is to remove #include "header2.h" and replace it with a forward declaration of the thread class.

    Code:
    class thread;
    
    class session {
    public:
    	session() { }
    	thread *threads;
    	thread *get_thread(unsigned short id);
    	..etc..
    };
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks guys, it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Running Program After Compiling
    By kristentx in forum C++ Programming
    Replies: 13
    Last Post: 09-12-2007, 10:46 AM
  2. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. compiling problem
    By tinkerbell20 in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2005, 12:12 PM
  5. simple compiling problem
    By waxydock in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2005, 10:33 AM