Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Interdependent classes

Interdependent classes

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CoolASL
    wrote on last edited by
    #1

    Hi all, Please help me out of this problem. I have 3 classes which all require each other. The problem is that they fail to recognize each other on compilation and give errors as: Undefined class Two, Undefined class Three and Undefined class One Someone suggested me forward declaration. But that too did not help. Where am i going wrong ? Please help me. Here is my code.. (Actually i am having the same problem with an VC++ program. This is just to simulate the same problem) File1.h

    class One
    {
    private:
    	int one;
    public:
    	int GetOne();
    	Two tw;
    	Three th;
    };
    

    File2.h class Two { private: int two; public: int GetTheTwo(); One on; Three th; }; File3.h class Three { private: int three; public: int GetAslThree(); One on; Two tw; }; File1.cpp #ifndef _FILE1_H #define _FILE1_H #include "File1.h" #endif int One::GetOne() { return one; } File2.cpp #ifndef _FILE2_H #define _FILE2_H #include "File2.h" #endif int Two::GetTheTwo() { return this->two; } File3.cpp #ifndef _FILE3_H #define _FILE3_H #include "File3.h" #endif int Three::GetAslThree() { return this->three; } Thanks in advance.

    *** Who said nothing is impossible? I have been doing it for a long time ***

    M C Z 3 Replies Last reply
    0
    • C CoolASL

      Hi all, Please help me out of this problem. I have 3 classes which all require each other. The problem is that they fail to recognize each other on compilation and give errors as: Undefined class Two, Undefined class Three and Undefined class One Someone suggested me forward declaration. But that too did not help. Where am i going wrong ? Please help me. Here is my code.. (Actually i am having the same problem with an VC++ program. This is just to simulate the same problem) File1.h

      class One
      {
      private:
      	int one;
      public:
      	int GetOne();
      	Two tw;
      	Three th;
      };
      

      File2.h class Two { private: int two; public: int GetTheTwo(); One on; Three th; }; File3.h class Three { private: int three; public: int GetAslThree(); One on; Two tw; }; File1.cpp #ifndef _FILE1_H #define _FILE1_H #include "File1.h" #endif int One::GetOne() { return one; } File2.cpp #ifndef _FILE2_H #define _FILE2_H #include "File2.h" #endif int Two::GetTheTwo() { return this->two; } File3.cpp #ifndef _FILE3_H #define _FILE3_H #include "File3.h" #endif int Three::GetAslThree() { return this->three; } Thanks in advance.

      *** Who said nothing is impossible? I have been doing it for a long time ***

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      Forward declaration can only be used for pointer types because the concrete definitions of the types have not been known yet in the compilation scope.

      class One;
      class Two;
      class Three{
      private:
      int three;
      public:
      int GetAslThree();
      One* pon;
      Two* ptw;
      };


      Maxwell Chen

      C 1 Reply Last reply
      0
      • C CoolASL

        Hi all, Please help me out of this problem. I have 3 classes which all require each other. The problem is that they fail to recognize each other on compilation and give errors as: Undefined class Two, Undefined class Three and Undefined class One Someone suggested me forward declaration. But that too did not help. Where am i going wrong ? Please help me. Here is my code.. (Actually i am having the same problem with an VC++ program. This is just to simulate the same problem) File1.h

        class One
        {
        private:
        	int one;
        public:
        	int GetOne();
        	Two tw;
        	Three th;
        };
        

        File2.h class Two { private: int two; public: int GetTheTwo(); One on; Three th; }; File3.h class Three { private: int three; public: int GetAslThree(); One on; Two tw; }; File1.cpp #ifndef _FILE1_H #define _FILE1_H #include "File1.h" #endif int One::GetOne() { return one; } File2.cpp #ifndef _FILE2_H #define _FILE2_H #include "File2.h" #endif int Two::GetTheTwo() { return this->two; } File3.cpp #ifndef _FILE3_H #define _FILE3_H #include "File3.h" #endif int Three::GetAslThree() { return this->three; } Thanks in advance.

        *** Who said nothing is impossible? I have been doing it for a long time ***

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        What Maxwell said is correct, so you can put class Class1; class Class2; at the top of Class3, and include the headers in the CPP, so long as Class3 returns, accepts and stores only pointers to Class1 and Class2. Then the compiler only needs to know that Class1 and Class2 exist, and that it needs to allocate storage for a pointer.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        C 1 Reply Last reply
        0
        • C Christian Graus

          What Maxwell said is correct, so you can put class Class1; class Class2; at the top of Class3, and include the headers in the CPP, so long as Class3 returns, accepts and stores only pointers to Class1 and Class2. Then the compiler only needs to know that Class1 and Class2 exist, and that it needs to allocate storage for a pointer.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          C Offline
          C Offline
          CoolASL
          wrote on last edited by
          #4

          Hi Maxwell/Christian, Thanks a lot guys for the quick reply. That really helped me. If i have a situation where i do need to declare an object rather than a pointer, then is there any possible way for it. Is it possible that i declare the pointer inside the class, and in the constructor of the class, i allocate memory for it. Will, that work. Or is there any other way for it. Since i am having different header files and CPP files, is it possible to include the files in such a way so that the declaration of a class appears before where it is required. But i guess, thats not possible with three classes. Thanks a lot friends..!!!

          *** Who said nothing is impossible? I have been doing it for a long time ***

          C 1 Reply Last reply
          0
          • M Maxwell Chen

            Forward declaration can only be used for pointer types because the concrete definitions of the types have not been known yet in the compilation scope.

            class One;
            class Two;
            class Three{
            private:
            int three;
            public:
            int GetAslThree();
            One* pon;
            Two* ptw;
            };


            Maxwell Chen

            C Offline
            C Offline
            CoolASL
            wrote on last edited by
            #5

            Hi Maxwell, Thanks a lot for the quick reply. That really helped me. But i have a situation where i do need to declare an object rather than a pointer, is there any possible way for it? Is it possible that i declare the pointer inside the class, and in the constructor of the class, i allocate memory for it. I am not able to make that work. Is there any other way for it. Thanks in advance !!

            *** Who said nothing is impossible? I have been doing it for a long time ***

            1 Reply Last reply
            0
            • C CoolASL

              Hi all, Please help me out of this problem. I have 3 classes which all require each other. The problem is that they fail to recognize each other on compilation and give errors as: Undefined class Two, Undefined class Three and Undefined class One Someone suggested me forward declaration. But that too did not help. Where am i going wrong ? Please help me. Here is my code.. (Actually i am having the same problem with an VC++ program. This is just to simulate the same problem) File1.h

              class One
              {
              private:
              	int one;
              public:
              	int GetOne();
              	Two tw;
              	Three th;
              };
              

              File2.h class Two { private: int two; public: int GetTheTwo(); One on; Three th; }; File3.h class Three { private: int three; public: int GetAslThree(); One on; Two tw; }; File1.cpp #ifndef _FILE1_H #define _FILE1_H #include "File1.h" #endif int One::GetOne() { return one; } File2.cpp #ifndef _FILE2_H #define _FILE2_H #include "File2.h" #endif int Two::GetTheTwo() { return this->two; } File3.cpp #ifndef _FILE3_H #define _FILE3_H #include "File3.h" #endif int Three::GetAslThree() { return this->three; } Thanks in advance.

              *** Who said nothing is impossible? I have been doing it for a long time ***

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              You really should rethink your design here. Having classes that are inter-dependent is not easy to write, and become impossible to maintain. There are tons of options you can take for making this code cleaner, but I will only provide one here:

              class Two;
              class Three;
              
              class One
              {
              private:
              	int _One;
              public:
              	std::tr1::shared_ptr<Two*> _Two;
              	std::tr1::shared_ptr<Three*> _Three;
              
              	One() : _Two(new Two), _Three(new Three)
              	{
              	}	
              };
              
              class Two
              {
              private:
              	int _Two;
              public:
              	std::tr1::shared_ptr<One*> _One;
              	std::tr1::shared_ptr<Three*> _Three;
              
              	Two() : _One(new One), _Three(new Three)
              	{
              	}
              };
              
              class Three
              {
              private:
              	int _Three;
              public:
              	std::tr1::shared_ptr<One*> _One;
              	std::tr1::shared_ptr<Two*> _Two;
              
              	Three() : _One(new One), _Two(new Two)
              	{
              	}
              };
              

              The best solution, however, would be to redesign these classes such that they are not inter-dependent.

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              B C 2 Replies Last reply
              0
              • C CoolASL

                Hi Maxwell/Christian, Thanks a lot guys for the quick reply. That really helped me. If i have a situation where i do need to declare an object rather than a pointer, then is there any possible way for it. Is it possible that i declare the pointer inside the class, and in the constructor of the class, i allocate memory for it. Will, that work. Or is there any other way for it. Since i am having different header files and CPP files, is it possible to include the files in such a way so that the declaration of a class appears before where it is required. But i guess, thats not possible with three classes. Thanks a lot friends..!!!

                *** Who said nothing is impossible? I have been doing it for a long time ***

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                The only other option, as someone suggested, is a redesign. Think about what makes these classes interdependent. Can you create a class which acts as a proxy between the two ?

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                C 1 Reply Last reply
                0
                • Z Zac Howland

                  You really should rethink your design here. Having classes that are inter-dependent is not easy to write, and become impossible to maintain. There are tons of options you can take for making this code cleaner, but I will only provide one here:

                  class Two;
                  class Three;
                  
                  class One
                  {
                  private:
                  	int _One;
                  public:
                  	std::tr1::shared_ptr<Two*> _Two;
                  	std::tr1::shared_ptr<Three*> _Three;
                  
                  	One() : _Two(new Two), _Three(new Three)
                  	{
                  	}	
                  };
                  
                  class Two
                  {
                  private:
                  	int _Two;
                  public:
                  	std::tr1::shared_ptr<One*> _One;
                  	std::tr1::shared_ptr<Three*> _Three;
                  
                  	Two() : _One(new One), _Three(new Three)
                  	{
                  	}
                  };
                  
                  class Three
                  {
                  private:
                  	int _Three;
                  public:
                  	std::tr1::shared_ptr<One*> _One;
                  	std::tr1::shared_ptr<Two*> _Two;
                  
                  	Three() : _One(new One), _Two(new Two)
                  	{
                  	}
                  };
                  

                  The best solution, however, would be to redesign these classes such that they are not inter-dependent.

                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                  B Offline
                  B Offline
                  BadKarma
                  wrote on last edited by
                  #8

                  Hi, I'm not sure if this will work. I think that this will surely blow your memory into oblivion. When you create an One object it will create a Two and a Three object. Those will create 2 One objects a Three and a Two object. These four objects will create on there turn 3 Two, 3 Three and 2 One objects and so further and so on ... Its like you said very dangerous to desing an inter-dependen class relation !!!

                  codito ergo sum

                  C Z 2 Replies Last reply
                  0
                  • C Christian Graus

                    The only other option, as someone suggested, is a redesign. Think about what makes these classes interdependent. Can you create a class which acts as a proxy between the two ?

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    C Offline
                    C Offline
                    CoolASL
                    wrote on last edited by
                    #9

                    Thanks Christian !!

                    *** Who said nothing is impossible? I have been doing it for a long time ***

                    1 Reply Last reply
                    0
                    • Z Zac Howland

                      You really should rethink your design here. Having classes that are inter-dependent is not easy to write, and become impossible to maintain. There are tons of options you can take for making this code cleaner, but I will only provide one here:

                      class Two;
                      class Three;
                      
                      class One
                      {
                      private:
                      	int _One;
                      public:
                      	std::tr1::shared_ptr<Two*> _Two;
                      	std::tr1::shared_ptr<Three*> _Three;
                      
                      	One() : _Two(new Two), _Three(new Three)
                      	{
                      	}	
                      };
                      
                      class Two
                      {
                      private:
                      	int _Two;
                      public:
                      	std::tr1::shared_ptr<One*> _One;
                      	std::tr1::shared_ptr<Three*> _Three;
                      
                      	Two() : _One(new One), _Three(new Three)
                      	{
                      	}
                      };
                      
                      class Three
                      {
                      private:
                      	int _Three;
                      public:
                      	std::tr1::shared_ptr<One*> _One;
                      	std::tr1::shared_ptr<Two*> _Two;
                      
                      	Three() : _One(new One), _Two(new Two)
                      	{
                      	}
                      };
                      

                      The best solution, however, would be to redesign these classes such that they are not inter-dependent.

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      C Offline
                      C Offline
                      CoolASL
                      wrote on last edited by
                      #10

                      thanks Zac.

                      *** Who said nothing is impossible? I have been doing it for a long time ***

                      1 Reply Last reply
                      0
                      • B BadKarma

                        Hi, I'm not sure if this will work. I think that this will surely blow your memory into oblivion. When you create an One object it will create a Two and a Three object. Those will create 2 One objects a Three and a Two object. These four objects will create on there turn 3 Two, 3 Three and 2 One objects and so further and so on ... Its like you said very dangerous to desing an inter-dependen class relation !!!

                        codito ergo sum

                        C Offline
                        C Offline
                        CoolASL
                        wrote on last edited by
                        #11

                        Thanks BadKarma... but that will make it quite complicated. Thanks anyway.:)

                        *** Who said nothing is impossible? I have been doing it for a long time ***

                        1 Reply Last reply
                        0
                        • B BadKarma

                          Hi, I'm not sure if this will work. I think that this will surely blow your memory into oblivion. When you create an One object it will create a Two and a Three object. Those will create 2 One objects a Three and a Two object. These four objects will create on there turn 3 Two, 3 Three and 2 One objects and so further and so on ... Its like you said very dangerous to desing an inter-dependen class relation !!!

                          codito ergo sum

                          Z Offline
                          Z Offline
                          Zac Howland
                          wrote on last edited by
                          #12

                          BadKarma wrote:

                          I think that this will surely blow your memory into oblivion.

                          He is going to have that problem anyway, it will just be on the stack instead of the heap (if he was actually able to do what he wanted to do, that is).

                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups