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

Re: Require help to solve!!!

 
0
  #11
May 29th, 2009
Presently my files are:
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. class TestA;
  3. class TestB {
  4. public:
  5. TestB();
  6. TestA* a;
  7. int id;
  8. void testPrint();
  9. void test();
  10. void set(TestA* t);
  11. };
testB.C
  1. #include "testB.h"
  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. }

I am able to compile g++ -c testA.C without any problems.
But g++ -c testB.C is giving multiple declarations error!!
How to fix this issue??

Thanks!!!
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
  #12
May 29th, 2009
Include testA.h in testB.h if you want it to have a pointer to a testA object
All i've got is a slice of pi
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
  #13
May 29th, 2009
Sorry!!!
It wont work that way since I have already included testB.h in testA.h and so can't include again the other way.
I initially posted this problem but then someone corrected me and now I am able to compile testA.C but not testB.C
If you go through the entire thread trail then you will come to know!!!
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
  #14
May 29th, 2009
You should always include the header when you want to use it's class to avoid multiple inclusion add the following code.

#ifdef TESTB_H
#define TESTB_H

//class declaration

#endif

And remove testA declaration in testB.h
All i've got is a slice of pi
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
  #15
May 29th, 2009
Ok...
My files are like this now:
testA.h
  1. #ifndef __TESTA_H
  2. #define __TESTA_H
  3.  
  4. #include <stdio.h>
  5. #include "testB.h"
  6.  
  7. class TestA {
  8. public:
  9. TestA();
  10. void testPrint();
  11. void test();
  12. void set(TestB* t);
  13. int getID();
  14.  
  15. private:
  16. int id;
  17. TestB* b;
  18. };
  19.  
  20. #endif
testB.h
  1. #ifndef __TESTB_H
  2. #define __TESTB_H
  3.  
  4. #include <stdio.h>
  5. #include "testA.h"
  6.  
  7. class TestB {
  8. public:
  9. TestB();
  10. void testPrint();
  11. void test();
  12. void set(TestA* t);
  13. int getID();
  14.  
  15. private:
  16. int id;
  17. TestA* a;
  18. };
  19.  
  20. #endif

On compiling g++ -Wall -c testA.C

testB.h:12: error: ‘TestA’ has not been declared
testB.h:17: error: ISO C++ forbids declaration of ‘TestA’ with no type
testB.h:17: error: expected ‘;’ before ‘*’ token


On compiling g++ -Wall -c testB.C

testA.h:12: error: ‘TestB’ has not been declared
testA.h:17: error: ISO C++ forbids declaration of ‘TestB’ with no type
testA.h:17: error: expected ‘;’ before ‘*’ token


Please help to fix this issue!!!
Last edited by abhishek2301; May 29th, 2009 at 3:45 pm.
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
  #16
May 29th, 2009
Are you sure you want to have a pointer of the other class in them both?
All i've got is a slice of pi
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
  #17
May 29th, 2009
Yes...that's my primary requirement for this post.
Otherwise I could have dealt with this situation!!!
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!!!

 
1
  #18
May 29th, 2009
OK last try
  1. class TestA;
  2. #ifndef __TESTA_H
  3. #define __TESTA_H
  4.  
  5. #include <stdio.h>
  6. #include "testB.h"
  7.  
  8. class TestA {
  9. public:
  10. TestA();
  11. void testPrint();
  12. void test();
  13. void set(TestB* t);
  14. int getID();
  15.  
  16. private:
  17. int id;
  18. TestB* b;
  19. };
  20.  
  21. #endif
All i've got is a slice of pi
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
  #19
May 29th, 2009
It's working!!!
Thanks a lot...You really fixed my issue
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: mirfan00 has a little shameless behaviour in the past 
Solved Threads: 2
mirfan00 mirfan00 is offline Offline
Light Poster

Re: Require help to solve!!!

 
0
  #20
May 29th, 2009
Dear you make two header files. One as a setA.h and other one as a setB.h. Now include both of the header files in your cpp file.
#include<iostream>
#include"setA.h"
#include"setB.h"

I hope you understand my wording.
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