Whats wrong with this code? (nested classes)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfonso Morra

    #1

    Whats wrong with this code? (nested classes)

    I have a class that contains a nested class. The outer class is called
    outer, and the nested class is called inner. When I try to compile the
    following code, I get a number of errors. It is not obvious to me, where
    I'm going wrong (the compiler messages do not seem to make much sense).

    here is the code:

    outer class declared as ff in "outer.h":


    #include "inner.h"

    class outer {
    public:
    outer() ;
    ~outer() ;
    private:
    class inner ;
    inner m_inner ;
    public:
    void dothis(void){ m_inner.dothis( ) ; }
    void dothat(void){ m_inner.dothat( ) ; }
    };


    inner class declared as follows in "inner.h" :

    #include "outer.h"

    class outer::inner {
    friend class outer ;

    outer::inner() ;
    ~outer::inner() ;

    void dothis( void ) {} ;
    void dothat( void ){} ;
    }


    Here is main() function:

    #include "outer.h"
    #include "inner.h"

    int main(int argc, char* argv[]) {

    outer y ;
    y.dothis() ;
    y.dothat() ;

    }


    I will be very grateful for advice to help me fix this as I spent a
    large portion of yesterday trying to fix this by reffering to various
    documentation - none of ehich actually addresses the issue of using or
    delegating to a nested class as I'm trying to do above. MTIA


  • Nishant Sivakumar

    #2
    Re: Whats wrong with this code? (nested classes)

    Hmmm, inner.h includes outer.h and outer.h includes inner.h <-- a dumb
    compiler would go into an infinite loop - a smart one would tell you that
    you cannot do that.

    --
    Regards,
    Nish [VC++ MVP]
    Nish’s thoughts on technology leadership, cloud architecture, and APIs, among other things




    "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
    news:dbifbo$ils $1@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
    >I have a class that contains a nested class. The outer class is called
    >outer, and the nested class is called inner. When I try to compile the
    >following code, I get a number of errors. It is not obvious to me, where
    >I'm going wrong (the compiler messages do not seem to make much sense).
    >
    > here is the code:
    >
    > outer class declared as ff in "outer.h":
    >
    >
    > #include "inner.h"
    >
    > class outer {
    > public:
    > outer() ;
    > ~outer() ;
    > private:
    > class inner ;
    > inner m_inner ;
    > public:
    > void dothis(void){ m_inner.dothis( ) ; }
    > void dothat(void){ m_inner.dothat( ) ; }
    > };
    >
    >
    > inner class declared as follows in "inner.h" :
    >
    > #include "outer.h"
    >
    > class outer::inner {
    > friend class outer ;
    >
    > outer::inner() ;
    > ~outer::inner() ;
    >
    > void dothis( void ) {} ;
    > void dothat( void ){} ;
    > }
    >
    >
    > Here is main() function:
    >
    > #include "outer.h"
    > #include "inner.h"
    >
    > int main(int argc, char* argv[]) {
    >
    > outer y ;
    > y.dothis() ;
    > y.dothat() ;
    >
    > }
    >
    >
    > I will be very grateful for advice to help me fix this as I spent a large
    > portion of yesterday trying to fix this by reffering to various
    > documentation - none of ehich actually addresses the issue of using or
    > delegating to a nested class as I'm trying to do above. MTIA
    >
    >[/color]


    Comment

    • Jeff Partch [MVP]

      #3
      Re: Whats wrong with this code? (nested classes)

      "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
      news:dbifbo$ils $1@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
      > I have a class that contains a nested class. The outer class is called
      > outer, and the nested class is called inner. When I try to compile the
      > following code, I get a number of errors. It is not obvious to me, where
      > I'm going wrong (the compiler messages do not seem to make much sense).
      >
      > here is the code:
      >
      > outer class declared as ff in "outer.h":
      >
      >
      > #include "inner.h"
      >
      > class outer {
      > public:
      > outer() ;
      > ~outer() ;
      > private:
      > class inner ;
      > inner m_inner ;
      > public:
      > void dothis(void){ m_inner.dothis( ) ; }
      > void dothat(void){ m_inner.dothat( ) ; }
      > };
      >
      >
      > inner class declared as follows in "inner.h" :
      >
      > #include "outer.h"
      >
      > class outer::inner {
      > friend class outer ;
      >
      > outer::inner() ;
      > ~outer::inner() ;
      >
      > void dothis( void ) {} ;
      > void dothat( void ){} ;
      > }
      >
      >
      > Here is main() function:
      >
      > #include "outer.h"
      > #include "inner.h"
      >
      > int main(int argc, char* argv[]) {
      >
      > outer y ;
      > y.dothis() ;
      > y.dothat() ;
      >
      > }
      >
      >
      > I will be very grateful for advice to help me fix this as I spent a
      > large portion of yesterday trying to fix this by reffering to various
      > documentation - none of ehich actually addresses the issue of using or
      > delegating to a nested class as I'm trying to do above. MTIA[/color]

      I think you can do it like this....

      class outer {
      public:
      outer() ;
      ~outer() ;
      private:
      class inner {
      friend class outer;
      inner();
      ~inner();
      void dothis( void ) {} ;
      void dothat( void ){} ;
      } m_inner;
      public:
      void dothis(void){ m_inner.dothis( ) ; }
      void dothat(void){ m_inner.dothat( ) ; }
      };

      --
      Jeff Partch [VC++ MVP]


      Comment

      Working...