943,754 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 786
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
May 29th, 2009
0

Re: Require help to solve!!!

Presently my files are:
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. 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
C++ Syntax (Toggle Plain Text)
  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!!!
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!!!

Include testA.h in testB.h if you want it to have a pointer to a testA object
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 29th, 2009
0

Re: Require help to solve!!!

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!!!
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!!!

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
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 29th, 2009
0

Re: Require help to solve!!!

Ok...
My files are like this now:
testA.h
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
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!!!

Are you sure you want to have a pointer of the other class in them both?
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 29th, 2009
0

Re: Require help to solve!!!

Yes...that's my primary requirement for this post.
Otherwise I could have dealt with this situation!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhishek2301 is offline Offline
24 posts
since Nov 2007
May 29th, 2009
1

Re: Require help to solve!!!

OK last try
CPP Syntax (Toggle Plain Text)
  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
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 29th, 2009
0

Re: Require help to solve!!!

It's working!!!
Thanks a lot...You really fixed my issue
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!!!

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.
Reputation Points: -9
Solved Threads: 2
Light Poster
mirfan00 is offline Offline
29 posts
since May 2009

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