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);
}

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

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

class TestB;
class TestA {
  // stuff
};
class TestB {
  // stuff
};
// methods

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

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.

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

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.

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

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

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

//definition
void foo::bar()
{
}

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

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

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

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

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

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

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.

This article has been dead for over six months. Start a new discussion instead.