Require help to solve!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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

Require help to solve!!!

 
0
  #1
May 28th, 2009
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!!!

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Require help to solve!!!

 
0
  #2
May 28th, 2009
Right before class TestA put this line

class TestB;
Last edited by Salem; May 28th, 2009 at 1:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: abhishek2301 is an unknown quantity at this point 
Solved Threads: 0
abhishek2301 abhishek2301 is offline Offline
Newbie Poster

Re: Require help to solve!!!

 
0
  #3
May 28th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Require help to solve!!!

 
0
  #4
May 28th, 2009
Well it won't because it doesn't know what TestB is yet.

  1. class TestB;
  2. class TestA {
  3. // stuff
  4. };
  5. class TestB {
  6. // stuff
  7. };
  8. // methods
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: abhishek2301 is an unknown quantity at this point 
Solved Threads: 0
abhishek2301 abhishek2301 is offline Offline
Newbie Poster

Re: Require help to solve!!!

 
0
  #5
May 28th, 2009
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:
  1. #include "testB.C"
  2. class testA {
  3. // stuff
  4. };

testB.C should look like:
  1. #include "testA.C"
  2. class testB {
  3. // stuff
  4. };

But the above method gives error for multiple declarations.
How to fix this issue???
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Require help to solve!!!

 
0
  #6
May 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: abhishek2301 is an unknown quantity at this point 
Solved Threads: 0
abhishek2301 abhishek2301 is offline Offline
Newbie Poster

Re: Require help to solve!!!

 
0
  #7
May 28th, 2009
Well,
My files are like this:
testA.h
  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
  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
  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
  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!!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Require help to solve!!!

 
0
  #8
May 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: abhishek2301 is an unknown quantity at this point 
Solved Threads: 0
abhishek2301 abhishek2301 is offline Offline
Newbie Poster

Re: Require help to solve!!!

 
0
  #9
May 28th, 2009
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!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: Require help to solve!!!

 
0
  #10
May 29th, 2009
put the declaration in the header file and the definition in the other

  1. //declaration
  2. class foo
  3. {
  4. void bar();
  5. };
  6.  
  7. //definition
  8. void foo::bar()
  9. {
  10. }
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC