943,817 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 786
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 28th, 2009
0

Require help to solve!!!

Expand Post »
Hello,
I require a help on a basic issue.
I have 2 classes and I want to have an interaction among the classes.
Suppose my 2 classes are testA and testB. Now I want a handle of object pointer in each class to the other class. Like I want a variable in testA to be testB* t and a variable in testB to be testA* t.
How can I implement this situation???
I have included the code for easy understanding. This code doesn't work since it is not able to resolve the declarations.
Your help is highly appreciated!!!

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. class TestA {
  4. public:
  5. TestA();
  6. int id;
  7. TestB* b;
  8. void testPrint();
  9. void test();
  10. void set(TestB* t);
  11. };
  12.  
  13. void TestA::testPrint() {
  14. printf("I am in class TestA\n");
  15. }
  16.  
  17. TestA::TestA() {
  18. id = 1;
  19. }
  20.  
  21. void TestA::set(TestB* t) {
  22. b = t;
  23. }
  24.  
  25. void TestB::test() {
  26. printf("My ID is: %d and the other ID is: %d\n", this->id, b->id);
  27. }
  28.  
  29. class TestB {
  30. public:
  31. TestB();
  32. TestA* a;
  33. int id;
  34. void testPrint();
  35. void test();
  36. void set(TestA* t);
  37. };
  38.  
  39. void TestB::testPrint() {
  40. printf("I am in class TestB\n");
  41. }
  42.  
  43. TestB::TestB() {
  44. id = 2;
  45. }
  46.  
  47. void TestB::set(TestA* t) {
  48. a = t;
  49. }
  50.  
  51. void TestB::test() {
  52. printf("My ID is: %d and the other ID is: %d\n", this->id, a->id);
  53. }
Last edited by abhishek2301; May 28th, 2009 at 12:21 pm. Reason: wrong code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhishek2301 is offline Offline
24 posts
since Nov 2007
May 28th, 2009
0

Re: Require help to solve!!!

Right before class TestA put this line

class TestB;
Last edited by Salem; May 28th, 2009 at 1:12 pm.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 28th, 2009
0

Re: Require help to solve!!!

I don't think it will solve the problem.
Check the output:
testA.C:26: error: invalid use of undefined type ‘struct TestB’
testA.C:3: error: forward declaration of ‘struct TestB’
testA.C: In member function ‘void TestB::test()’:
testA.C:27: error: invalid use of undefined type ‘struct TestB’
testA.C:3: error: forward declaration of ‘struct TestB’
testA.C:27: error: ‘b’ was not declared in this scope
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhishek2301 is offline Offline
24 posts
since Nov 2007
May 28th, 2009
0

Re: Require help to solve!!!

Well it won't because it doesn't know what TestB is yet.

C++ Syntax (Toggle Plain Text)
  1. class TestB;
  2. class TestA {
  3. // stuff
  4. };
  5. class TestB {
  6. // stuff
  7. };
  8. // methods
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 28th, 2009
0

Re: Require help to solve!!!

Thanks very much!!!
I need a little more help.
If I want to place testA.C and testB.C in separate files then how to do it???
Like the following:
testA.C should look like:
C++ Syntax (Toggle Plain Text)
  1. #include "testB.C"
  2. class testA {
  3. // stuff
  4. };

testB.C should look like:
C++ Syntax (Toggle Plain Text)
  1. #include "testA.C"
  2. class testB {
  3. // stuff
  4. };

But the above method gives error for multiple declarations.
How to fix this issue???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhishek2301 is offline Offline
24 posts
since Nov 2007
May 28th, 2009
0

Re: Require help to solve!!!

Well the classes themselves go in
TestA.h and TestB.h

The actual implementations go in
TestA.cpp and TestB.cpp

Further, when you do something like
b->id
within an 'A' member function, you should really use something like
b->getID()
so that later on, when you make all the data private, your code will still work.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 28th, 2009
0

Re: Require help to solve!!!

Well,
My files are like this:
testA.h
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include "testB.h"
  3.  
  4. class TestA {
  5. public:
  6. TestA();
  7. int id;
  8. TestB* b;
  9. void testPrint();
  10. void test();
  11. void set(TestB* t);
  12. };
testA.C
C++ Syntax (Toggle Plain Text)
  1. #include "testA.h"
  2.  
  3. void TestA::testPrint() {
  4. printf("I am in class TestA\n");
  5. }
  6.  
  7. TestA::TestA() {
  8. id = 1;
  9. }
  10.  
  11. void TestA::set(TestB* t) {
  12. b = t;
  13. }
  14.  
  15. void TestA::test() {
  16. printf("My ID is: %d and the other ID is: %d\n", this->id, b->id);
  17. }
testB.h
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include "testA.h"
  3.  
  4. class TestB {
  5. public:
  6. TestB();
  7. TestA* a;
  8. int id;
  9. void testPrint();
  10. void test();
  11. void set(TestA* t);
  12. };
testB.C
C++ Syntax (Toggle Plain Text)
  1. #include "testB.C"
  2.  
  3. void TestB::testPrint() {
  4. printf("I am in class TestB\n");
  5. }
  6.  
  7. TestB::TestB() {
  8. id = 2;
  9. }
  10.  
  11. void TestB::set(TestA* t) {
  12. a = t;
  13. }
  14.  
  15. void TestB::test() {
  16. printf("My ID is: %d and the other ID is: %d\n", this->id, a->id);
  17. }

On compiling
g++ -c testA.C or g++ -c testB.C
giving many multiple declaration errors!!!
Please help me to fix this issue!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhishek2301 is offline Offline
24 posts
since Nov 2007
May 28th, 2009
0

Re: Require help to solve!!!

You didn't follow my replies.

ONE of your header files needs the class forward declaration.
AND both of them should NOT include the other.

In my original example, TestB.h could include TestA.h, but not vice-versa.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 28th, 2009
0

Re: Require help to solve!!!

Thanks a lot!!!
I think I am not considering dependencies.
OK..Now I am able to compile testA.C
But then how to compile testB.C
g++ -c testB.C gives error now!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhishek2301 is offline Offline
24 posts
since Nov 2007
May 29th, 2009
0

Re: Require help to solve!!!

put the declaration in the header file and the definition in the other

CPP Syntax (Toggle Plain Text)
  1. //declaration
  2. class foo
  3. {
  4. void bar();
  5. };
  6.  
  7. //definition
  8. void foo::bar()
  9. {
  10. }
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008

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: Please Hlpe me, lojec erooe
Next Thread in C++ Forum Timeline: image in picture control not displayed properly after moving





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


Follow us on Twitter


© 2011 DaniWeb® LLC