Whats wrong with this code? (nested classes)

A

Alfonso Morra

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
 
N

Nishant Sivakumar

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.
 
J

Jeff Partch [MVP]

Alfonso Morra said:
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

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() ; }
};
 

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

Top