How to cross reference objects?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 4
Reputation: Rick1980 is an unknown quantity at this point 
Solved Threads: 0
Rick1980 Rick1980 is offline Offline
Newbie Poster

How to cross reference objects?

 
0
  #1
Nov 17th, 2007
Hello All,

I know that there is a similar thread about my problem, but I can't find it anymore.
Well basically I want 2 objects to know from each other. I know that in C++ I have to
use pointers to do that. I wrote some sample code to show you the problem:
  1. #ifndef A_HPP_
  2. #define A_HPP_
  3. #include "B.hpp"
  4.  
  5. class A
  6. {
  7. public:
  8. //B* bpt;
  9. A();
  10. virtual ~A();
  11. private:
  12. B* bpt;
  13. };
  14. #endif /*A_HPP_*/
  15. =========================================
  16. #include "A.hpp"
  17. #include "B.hpp"
  18. A::A()
  19. {
  20. }
  21.  
  22. A::~A()
  23. {
  24. }
  25. ========================================
  26. #ifndef B_HPP_
  27. #define B_HPP_
  28. #include "A.hpp"
  29.  
  30. class B
  31. {
  32. public:
  33. //A* apt;
  34. B();
  35. virtual ~B();
  36. private:
  37. A* apt;
  38. };
  39. #endif /*B_HPP_*/
  40. =======================================
  41. #include "B.hpp"
  42. #include "A.hpp"
  43. B::B()
  44. {
  45. }
  46.  
  47. B::~B()
  48. {
  49. }
======================================
The error I get is:

**** Build of configuration Linux GCC for project TestCrossPt ****

make all
g++ -c A.cpp
B.hpp:13: error: ISO C++ forbids declaration of ‘A’ with no type
B.hpp:13: error: expected ‘;’ before ‘*’ token
make: *** [A.o] Error 1
======================================
And this is the makefile I use:

all: test

test: A.o B.o TestCrossPt.o
g++ A.o B.o TestCrossPt.o -o test.exe -lpthread

A.o: A.cpp
g++ -c A.cpp

B.o: B.hpp
g++ -c B.cpp

TestCrossPt.o: TestCrossPt.cpp
g++ -c TestCrossPt.cpp
==========================================
Well, I am pretty new to C++ so please use easy language

many thanks

Rick
Last edited by Ancient Dragon; Nov 17th, 2007 at 5:28 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to cross reference objects?

 
0
  #2
Nov 17th, 2007
The problem is that you are recursively including A.hpp and B.hpp. So solve the problem, just pre-declare the classes
#ifndef B_HPP_
#define B_HPP_
class A; 
#include "A.hpp" 
class B
{
public:	
      B();	
      virtual ~B();
 private:	
      A* apt;
};
#endif /*B_HPP_*/
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 4
Reputation: Rick1980 is an unknown quantity at this point 
Solved Threads: 0
Rick1980 Rick1980 is offline Offline
Newbie Poster

Re: How to cross reference objects?

 
0
  #3
Nov 17th, 2007
Brilliant, thank you very much for the quick and helpful answer
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC