looped include

D

Dragilla

Hi, I have a problem....
I can show you what this is about in an example:
There are 4 files:

a.c:
#include "a.h"

int main() {

B* b = new B();
A* a = new A();

a->bref = b;
b->aref = a;

b->aref->test();
a->bref->test();
return 0;
}

b.c:
#include "b.h"

a.h:
#ifndef A_H
#define A_H
#include "b.h"

#include <stdio.h>
#include <stdlib.h>

class B;
class A {
public: B* bref;
public: A() {;}
public: void test()
{
printf ("test in A\n");
bref->test();
}
};

#endif

b.h:
#ifndef B_H
#define B_H
#include "a.h"

#include <stdlib.h>
#include <stdio.h>

class A;
class B {
public: A* aref;
public: B() {;}
public: void test() { printf ("test in B\n"); }
};

#endif

Why does this not compile? I get errors in line with code: bref-
test(); in a.h
PLEASE help.

regards,
 
B

Barry

Dragilla said:
Hi, I have a problem....
I can show you what this is about in an example:
There are 4 files:

a.c:
#include "a.h"

int main() {

B* b = new B();
A* a = new A();

a->bref = b;
b->aref = a;

b->aref->test();
a->bref->test();
return 0;
}

b.c:
#include "b.h"

a.h:
#ifndef A_H
#define A_H
#include "b.h"

#include <stdio.h>
#include <stdlib.h>

class B;

// class B;
not needed
class A {
public: B* bref;
public: A() {;}
public: void test()
{
printf ("test in A\n");
bref->test();
}
};

#endif

b.h:
#ifndef B_H
#define B_H
#include "a.h"

//#include "a.h"
not needed
 
D

Dragilla

Ok, that worked, thanks alot.
But if I modify my files like you said and add some code, so that
class B looks like this:
class B {
public: A* aref;
public: B() {;}
public: void test()
{
printf ("test in B\n");
aref->test2;
}
};

and add function test2 to class A, i get a whole new bunch of
errors....

Actually my problem concerns Windows Forms i VC++ in .Net 2003.
I have a main form Form1 and 2 other forms i need to show/hide from
the main form.
Then i need the data from the other 2 forms back into the Form1.
I can't get it to work... Any standart solution to this?

regards,
 
R

Rolf Magnus

Dragilla said:
Ok, that worked, thanks alot.
But if I modify my files like you said and add some code, so that
class B looks like this:
class B {
public: A* aref;
public: B() {;}
public: void test()

Repeating "public:" multiple times doesn't get it more public than it
already is ;-)
{
printf ("test in B\n");
aref->test2;
}
};

and add function test2 to class A, i get a whole new bunch of
errors....

Don't put the implementation into the header file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top