By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

help?

P: 24
how should you solve this problem?

Expand|Select|Wrap|Line Numbers
  1. // from a header file
  2.  
  3. #ifndef LONG_AND_UNIQUE_NAME
  4. #define LONG_AND_UNIQUE_NAME
  5.  
  6. namespace name_of_package {
  7.  
  8.    class A {
  9.    public:
  10.       A(B *);
  11.       ...
  12.    private:
  13.       B *p_b;
  14.    }
  15.  
  16.    class B {
  17.        ...
  18.    private:
  19.       A *p_a;
  20.    }
  21. }
  22.  
  23.  
Jun 11 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
how should you solve this problem?

Expand|Select|Wrap|Line Numbers
  1. // from a header file
  2.  
  3. #ifndef LONG_AND_UNIQUE_NAME
  4. #define LONG_AND_UNIQUE_NAME
  5.  
  6. namespace name_of_package {
  7.  
  8.    class A {
  9.    public:
  10.       A(B *);
  11.       ...
  12.    private:
  13.       B *p_b;
  14.    }
  15.  
  16.    class B {
  17.        ...
  18.    private:
  19.       A *p_a;
  20.    }
  21. }
  22.  
  23.  
Did you try it yourself first. See the guidelines.
Jun 11 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
// 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.

Expand|Select|Wrap|Line Numbers
  1. // from a header file
  2.  
  3. #ifndef LONG_AND_UNIQUE_NAME
  4. #define LONG_AND_UNIQUE_NAME
  5.  
  6. namespace name_of_package {
  7.  
  8.    class B;  //forward reference
  9.  
  10.    class A {
  11.    public:
  12.       A(B *);
  13.       ...
  14.    private:
  15.       B *p_b;
  16.    }
  17.  
  18.    class B {
  19.        ...
  20.    private:
  21.       A *p_a;
  22.    }
  23. }
  24.  
Jun 11 '07 #3

P: 24
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.

:) :) :) :) :) :) :)
Jun 11 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.