Forward declaration problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • linq936@hotmail.com

    #1

    Forward declaration problem

    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?

  • linq936@hotmail.com

    #2
    Re: Forward declaration problem


    linq936@hotmail .com wrote:[color=blue]
    > 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?[/color]

    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.

    Comment

    Working...