help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stroken
    New Member
    • Mar 2007
    • 24

    help?

    how should you solve this problem?

    Code:
    // from a header file
    
    #ifndef LONG_AND_UNIQUE_NAME
    #define LONG_AND_UNIQUE_NAME
    
    namespace name_of_package {
    
       class A {
       public:
          A(B *);
          ...
       private:
          B *p_b;
       }
    
       class B {
           ...
       private:
          A *p_a;
       }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by stroken
    how should you solve this problem?

    Code:
    // from a header file
    
    #ifndef LONG_AND_UNIQUE_NAME
    #define LONG_AND_UNIQUE_NAME
    
    namespace name_of_package {
    
       class A {
       public:
          A(B *);
          ...
       private:
          B *p_b;
       }
    
       class B {
           ...
       private:
          A *p_a;
       }
    }
    Did you try it yourself first. See the guidelines.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by stroken
      // from a header file

      #ifndef LONG_AND_UNIQUE _NAME
      #define LONG_AND_UNIQUE _NAME

      namespace name_of_package {

      class A {
      public:
      A(B *);
      ...
      private:
      B *p_b;
      }

      class B {
      ...
      private:
      A *p_a;
      }
      }
      Use a forward reference. That's enough information for the compiler to know that class B exists and that is enough to allow a pointer to an object of class B.

      [code=cpp]
      // from a header file

      #ifndef LONG_AND_UNIQUE _NAME
      #define LONG_AND_UNIQUE _NAME

      namespace name_of_package {

      class B; //forward reference

      class A {
      public:
      A(B *);
      ...
      private:
      B *p_b;
      }

      class B {
      ...
      private:
      A *p_a;
      }
      }
      [/code]

      Comment

      • stroken
        New Member
        • Mar 2007
        • 24

        #4
        Originally posted by weaknessforcats
        Use a forward reference. That's enough information for the compiler to know that class B exists and that is enough to allow a pointer to an object of class B.
        Thanks.

        :) :) :) :) :) :) :)

        Comment

        Working...