944,058 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7931
  • C++ RSS
Nov 17th, 2007
0

How to cross reference objects?

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rick1980 is offline Offline
4 posts
since Nov 2007
Nov 17th, 2007
0

Re: How to cross reference objects?

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_*/
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Nov 17th, 2007
0

Re: How to cross reference objects?

Brilliant, thank you very much for the quick and helpful answer
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rick1980 is offline Offline
4 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: small problem in the string
Next Thread in C++ Forum Timeline: some errors with classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC