Results 1 to 11 of 11

Thread: Including Files

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Including Files

    I'm trying to do something like this:

    MainClass.h:
    Code:
    #ifndef __MAIN_CLASS_H
    #define __MAIN_CLASS_H
    
    #include "SecondaryClass.h"
    
    class MainClass
    {
    	private:
    		SecondaryClass *secondaryClass;
    		
    	public:
    		MainClass();
    		~MainClass();
    	
    };
    
    #endif
    SecondaryClass.h:
    Code:
    #ifndef __SECONDARY_CLASS_H
    #define __SECONDARY_CLASS_H
    
    #include "MainClass.h"
    
    class SecondaryClass
    {
    	private:
    		MainClass *m_mainClass;
    		
    	public:
    		SecondaryClass( MainClass* );		
    		~SecondaryClass();
    };
    
    #endif
    And I get the following errors:

    In file included from MainClass.h:4,
    from main.cpp:2:
    SecondaryClass.h:9: error: ISO C++ forbids declaration of `MainClass' with no type
    SecondaryClass.h:9: error: expected `;' before '*' token

    make.exe: *** [main.o] Error 1
    I'm sure this is because I am including a file that includes the file I'm currently in.

    The point of this being that MainClass has a member variable of Secondary class, which also has a reference to the MainClass that instantiated it.

    Any ideas on how to accomplish this?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Addicted Member Kal-El's Avatar
    Join Date
    Jun 2007
    Location
    Fortress of solitude
    Posts
    179

    Re: Including Files

    I think because never reashes to the end of the class...
    See, is like this:
    1) start header A.
    2) header A calls header B.
    3) start header B.
    4) header B calls header A.
    5) start header A.
    6) header A calls header B, again!!!! why I didn't end the step 1 header A ?
    ...

    Never reashes to the end of the class, because you are like inner loop or something the header over and over and over

    «Source Code»«plugins»«skin»«fav apps»«Winamp en español»«Nsis en español»
    So what if your signature move is driving a tractor? I think it's adorable. - Lois ("Crimson")
    Don't forget to when your question is resolved ...and the people who help

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Including Files

    I don't think that would be the case due to the #ifndef #endif block around the code. The class is only defined once -- the second time, the code in the block should be ignored. Correct?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Addicted Member Kal-El's Avatar
    Join Date
    Jun 2007
    Location
    Fortress of solitude
    Posts
    179

    Re: Including Files

    Correct! But see this:
    Even is "in used" the header, it never reashes the end of the header MainClass.

    «Source Code»«plugins»«skin»«fav apps»«Winamp en español»«Nsis en español»
    So what if your signature move is driving a tractor? I think it's adorable. - Lois ("Crimson")
    Don't forget to when your question is resolved ...and the people who help

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Including Files

    I have no idea what you are talking about.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Addicted Member Kal-El's Avatar
    Join Date
    Jun 2007
    Location
    Fortress of solitude
    Posts
    179

    Re: Including Files

    And I don't why are you using this kinda code

    «Source Code»«plugins»«skin»«fav apps»«Winamp en español»«Nsis en español»
    So what if your signature move is driving a tractor? I think it's adorable. - Lois ("Crimson")
    Don't forget to when your question is resolved ...and the people who help

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Including Files

    Is there something wrong with it?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Including Files

    I think what Kal-El means, is this:
    C++ Code:
    1. #ifndef __MAIN_CLASS_H
    2. #define __MAIN_CLASS_H
    3.  
    4. // #include "SecondaryClass.h"
    5.  
    6. class MainClass
    7. {
    8.     private:
    9.         SecondaryClass *secondaryClass;
    10.        
    11.     public:
    12.         MainClass();
    13.         ~MainClass();
    14.    
    15. };
    16.  
    17. #include "SecondaryClass.h" // Put it here, because the preprocessor will know what MainClass is now..
    18.  
    19. #endif
    Although, I'm not sure if that is correct.. I'll test it soon and get back to you.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Including Files

    Chemical, I do not believe I can do that, because MainClass needs to have the definition of SecondaryClass.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Including Files

    I've never tried something like this.. as I've never had the need to. What you might need, is a friend class to both of your current classes, which handles the information between the two.

    I'll stuff around with your code for a bit and get back to you.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  11. #11
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Including Files

    Actually.. silly me.

    Just forward declare the classes:

    MainClass.h

    C++ Code:
    1. #ifndef __MAIN_CLASS_H
    2. #define __MAIN_CLASS_H
    3.  
    4. #include "SecondaryClass.h"
    5.  
    6. class SecondaryClass; // <------
    7.  
    8. class MainClass
    9. {
    10.     private:
    11.         SecondaryClass *secondaryClass;
    12.        
    13.     public:
    14.         MainClass();
    15.         ~MainClass();
    16.    
    17. };
    18.  
    19. #endif

    SecondaryClass.h

    C++ Code:
    1. #ifndef __SECONDARY_CLASS_H
    2. #define __SECONDARY_CLASS_H
    3.  
    4. #include "MainClass.h"
    5.  
    6. class MainClass; // <--------
    7.  
    8. class SecondaryClass
    9. {
    10.     private:
    11.         MainClass *m_mainClass;
    12.        
    13.     public:
    14.         SecondaryClass( MainClass* );      
    15.         ~SecondaryClass();
    16. };
    17.  
    18. #endif

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width