Forward declaration problem

L

linq936

Hi,
I have 2 classes, h1 and h2, declared in 2 seperated header files h1.h
and h2.h and implementated in 2 seperated source files h1.c and h2.c.

The h1.h looks like this,

#ifndef H1_H
#define H1_H

#ifndef H2_H
#include "h2.h"
#endif

namespace ns {
class h1 {
<some stuff>
}
}
#endif


The h2.h looks like this,

#ifndef H2_H
#define H2_H

#include "h1.h"
// NOTICE that I include the header without condition

namespace ns {
class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

the h1.c looks like this,
#ifndef H1_H
#include "h1.h"
#endif

<some code>

when I compile h1.c using Visual C++ compiler I get an error, error
C2079: 'h1_obj' uses undefined class 'ns::h1'.

Then I updated h2.h by adding a forward declaration of class h1 like
this,
#ifndef H2_H
#define H2_H

#include "h1.h"

namespace ns {
class h1;

class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

But I get the same error.

Any idea?
 
L

linq936

Hi,
I have 2 classes, h1 and h2, declared in 2 seperated header files h1.h
and h2.h and implementated in 2 seperated source files h1.c and h2.c.

The h1.h looks like this,

#ifndef H1_H
#define H1_H

#ifndef H2_H
#include "h2.h"
#endif

namespace ns {
class h1 {
<some stuff>
}
}
#endif


The h2.h looks like this,

#ifndef H2_H
#define H2_H

#include "h1.h"
// NOTICE that I include the header without condition

namespace ns {
class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

the h1.c looks like this,
#ifndef H1_H
#include "h1.h"
#endif

<some code>

when I compile h1.c using Visual C++ compiler I get an error, error
C2079: 'h1_obj' uses undefined class 'ns::h1'.

Then I updated h2.h by adding a forward declaration of class h1 like
this,
#ifndef H2_H
#define H2_H

#include "h1.h"

namespace ns {
class h1;

class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

But I get the same error.

Any idea?

OK, solve the problem, I changed h1.h like this,

#ifndef H1_H
#define H1_H

namespace ns {
class h1 {
<some stuff>
}
}

#ifndef H2_H
#include "h2.h"
#endif

#endif

Now it is ok.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top