954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Require help to solve!!!

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

#include <stdio.h>

class TestA {
  public:
    TestA();
    int id; 
    TestB* b;
    void testPrint();
    void test();
    void set(TestB* t);
};

void TestA::testPrint() {
  printf("I am in class TestA\n");
}

TestA::TestA() {
  id = 1;
}

void TestA::set(TestB* t) {
  b = t;
}

void TestB::test() {
  printf("My ID is: %d and the other ID is: %d\n", this->id, b->id);
}

class TestB {
  public:
    TestB();
    TestA* a;
    int id;
    void testPrint();
    void test();
    void set(TestA* t);
};

void TestB::testPrint() {
  printf("I am in class TestB\n");
}

TestB::TestB() {
  id = 2;
}

void TestB::set(TestA* t) {
  a = t;
}

void TestB::test() {
  printf("My ID is: %d and the other ID is: %d\n", this->id, a->id);
}
abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Right before class TestA put this line class TestB;

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

class TestB;
class TestA {
  // stuff
};
class TestB {
  // stuff
};
// methods
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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:

#include "testB.C"
class testA {
// stuff
};


testB.C should look like:

#include "testA.C"
class testB {
// stuff
};


But the above method gives error for multiple declarations.
How to fix this issue???

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Well,
My files are like this:
testA.h

#include <stdio.h>
#include "testB.h"

class TestA {
  public:
    TestA();
    int id; 
    TestB* b;
    void testPrint();
    void test();
    void set(TestB* t);
};

testA.C

#include "testA.h"

void TestA::testPrint() {
  printf("I am in class TestA\n");
}

TestA::TestA() {
  id = 1;
}

void TestA::set(TestB* t) {
  b = t;
}

void TestA::test() {
  printf("My ID is: %d and the other ID is: %d\n", this->id, b->id);
}

testB.h

#include <stdio.h>
#include "testA.h"

class TestB {
  public:
    TestB();
    TestA* a;
    int id;
    void testPrint();
    void test();
    void set(TestA* t);
};

testB.C

#include "testB.C"

void TestB::testPrint() {
  printf("I am in class TestB\n");
}

TestB::TestB() {
  id = 2;
}

void TestB::set(TestA* t) {
  a = t;
}

void TestB::test() {
  printf("My ID is: %d and the other ID is: %d\n", this->id, a->id);
}


On compiling
g++ -c testA.C or g++ -c testB.C
giving many multiple declaration errors!!!
Please help me to fix this issue!!!

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

//declaration
class foo
{
    void bar();
};

//definition
void foo::bar()
{
}
mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

Presently my files are:
testA.h

#include <stdio.h>
#include "testB.h"

class TestA {
  public:
    TestA();
    int id; 
    TestB* b;
    void testPrint();
    void test();
    void set(TestB* t);
};

testA.C

#include "testA.h"

void TestA::testPrint() {
  printf("I am in class TestA\n");
}

TestA::TestA() {
  id = 1;
}

void TestA::set(TestB* t) {
  b = t;
}

void TestA::test() {
  printf("My ID is: %d and the other ID is: %d\n", this->id, b->id);
}

testB.h

#include <stdio.h>
class TestA;
class TestB {
  public:
    TestB();
    TestA* a;
    int id;
    void testPrint();
    void test();
    void set(TestA* t);
};

testB.C

#include "testB.h"

void TestB::testPrint() {
  printf("I am in class TestB\n");
}

TestB::TestB() {
  id = 2;
}

void TestB::set(TestA* t) {
  a = t;
}

void TestB::test() {
  printf("My ID is: %d and the other ID is: %d\n", this->id, a->id);
}


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

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Include testA.h in testB.h if you want it to have a pointer to a testA object

mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

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

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

Ok...
My files are like this now:
testA.h

#ifndef __TESTA_H
#define __TESTA_H

#include <stdio.h>
#include "testB.h"

class TestA {
  public:
    TestA();
    void testPrint();
    void test();
    void set(TestB* t);
    int getID();

  private:
    int id;
    TestB* b;
};

#endif

testB.h

#ifndef __TESTB_H
#define __TESTB_H

#include <stdio.h>
#include "testA.h"

class TestB {
  public:
    TestB();
    void testPrint();
    void test();
    void set(TestA* t);
    int getID();

  private:
    int id;
    TestA* a;
};

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

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Are you sure you want to have a pointer of the other class in them both?

mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

Yes...that's my primary requirement for this post.
Otherwise I could have dealt with this situation!!!

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

OK last try

class TestA;
#ifndef __TESTA_H
#define __TESTA_H

#include <stdio.h>
#include "testB.h"

class TestA {
  public:
    TestA();
    void testPrint();
    void test();
    void set(TestB* t);
    int getID();

  private:
    int id;
    TestB* b;
};

#endif
mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

It's working!!!
Thanks a lot...You really fixed my issue

abhishek2301
Light Poster
27 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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
#include"setA.h"
#include"setB.h"

I hope you understand my wording.

mirfan00
Light Poster
29 posts since May 2009
Reputation Points: -9
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You