Access violation Problem With Two Classes

Hello,
I have two classes that have for member variables pointers to objects of each other like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// class A.h
class B;

#include "class B.h"

class A 
{
   B *b;

  public:
   void doSomething () { something;}
   void useClassB () { b->doSomething; }
   friend class B;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// class B.h
class A;

#include "class A.h"

class B 
{
   A *a;

  public:
   void doSomething () { something;}
   void useClassA () { a->doSomething; }
   friend class A;
}

Now It compiles alright but then I get a few runtime errors:
1
2
First-chance exception at 0x00418f1c in program.exe: 0xC0000005: Access violation reading location 0xcccccd6c.
Unhandled exception at 0x00418f1c in program.exe: 0xC0000005: Access violation reading location 0xcccccd6c.

I'm not sure why this happens. Is anyone willing to help me understand the problem and show me a work-around? I'm not really sure how to make it were both classes are dependent on each other, and that's the problem.
Last edited on
The pointers aren't initialized.
Thanks for your time helios.

EDIT:
I've tried tried to initialize the two members in each others constructors to zero, but I'm still getting Access violation errors. I'm looking around on google, but I'm not yet sure how to get this going.
Last edited on
Zero won't do. You have to make them valid pointers by calling the constructor for their objects with new. Example: this->a=new A;
I'm looking at your setup, and if you do what I think you're thinking of doing, you'll end up with infinite recursion.
Yes, I'm getting a stack overflow.
I simplified the code in the example a lot, but I'm needing to make the two classes mutually available as members to each other. Are you saying this not possible because I'll get infinite recursion?

Should I try to redesign the two classes to have one as the parent and one the dependent?
EDIT -- blah I totally misread your question. That's what I get for jumping to conclusions. Sorry.

ANYWAY, if your goal is to create an A object and a B object which refer to each other, then you can pass 'this' to the other class as the pointer. It's best to have one class be the dominant one, that way cleanup and such is easier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// --- note that functions here are implicitly inlined to keep the example simple,
//  but you might not be able to do that in the actual program

class A
{
public:
  // ctor, which creates a new B partner
  A()
  {
    b = new B(this);  // give it 'this' as its partner
  }

  ~A()
  {
    delete b;
  }

protected:
  B* b;
};

//------------------
class B  // B is the passive partner of the dominant A class
{
public:
  // 
  B(A* partner) : a(partner) { }
  ~B()  { }  // do not delete a!  B is passive

protected:
  A* a;
};


In this example, whenever you create an A object, that A object will in turn create a B object, and they will be "tied" together in the sense that they will point to each other and be able to communicate back and forth.

But if you want A to have a B which has another A -- you're starting to step into the world of impossible concepts. After all if A has a B which has another A which has another B which has another A, etc, etc -- you'll end up creating an infinite number of objects.
Last edited on
Thanks Disch. I'll give this a try.

EDIT: after giving this a try it seems to work well, I really had no clue what I was doing, thanks.
Last edited on
Topic archived. No new replies allowed.