Whats wrong with this code? (nested classes question)

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
 
V

vindhya

Modify your code like this.


class outer {
public:
outer() ;
~outer() ;
private:
class inner ;
class inner{public: inner(); ~inner(); void dothis(); void
dothat();};
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"

outer::inner::inner() ;
~outer::inner::inner() ;
void outer::inner::dothis( void ) {} ;
void outer::inner::dothat( void ){} ;

}


The reason for this is :
1) Multiple looping is happening because of recursivenes in inclusion
of header files.
2)When you create an object from a class then you need to declare and
define the class. The implementation can be left and done seperately.
3)As mentioned in above point you also need to define dothis and dothat
before using them. Compiler is unaware of it when you try to acces
inner::dothis.

Is there any special reason to code the way you have done or is it just
for testing purpose.

MS
 
S

sugary.boy

#include<iostream.h>

class outer {
public:
outer(){cout<<"Constructing outer object"<<endl;}
~outer(){cout<<"Destructing outer object"<<endl;}
private:
class inner{
public:
inner(){cout<<"Constructing inner object"<<endl;}
~inner(){cout<<"Destructing inner object"<<endl;}

public:
void dothis( void ) {cout<<"Inside inner"<<endl;}
void dothat( void ){cout<<"Inside inner"<<endl;}
} m_inner ;
public:
void dothis(void){ cout<<"Inside outer"<<endl; m_inner.dothis()
; }
void dothat(void){ cout<<"Inside outer"<<endl; m_inner.dothat()
; }
};


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


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

return 0;
}

try this way...
 
A

Alfonso Morra

vindhya said:
Modify your code like this.


class outer {
public:
outer() ;
~outer() ;
private:
class inner ;
class inner{public: inner(); ~inner(); void dothis(); void
dothat();};
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"

outer::inner::inner() ;
~outer::inner::inner() ;
void outer::inner::dothis( void ) {} ;
void outer::inner::dothat( void ){} ;

}


The reason for this is :
1) Multiple looping is happening because of recursivenes in inclusion
of header files.

I figured there was some recursion going on but I wasn't sure on how to
eliminate it. Thanks for the example.
2)When you create an object from a class then you need to declare and
define the class. The implementation can be left and done seperately.

This is what I'm trying to do - but I also believe this is what lied at
the heart of the problems I was having. I wanted to seperate the classes
seperately into two classes, i.e. not "clutter" outer with too much of
inner's details - but then to "link" the two together via the headers.

It appears from your example, that I have to provide the _entire_ nested
class API (including private variables etc - if outer is to be a friend
of inner). Is this a correct inference from the code you provided?
3)As mentioned in above point you also need to define dothis and dothat
before using them. Compiler is unaware of it when you try to acces
inner::dothis.

I got that. Thanks.
Is there any special reason to code the way you have done or is it just
for testing purpose.

The main reason, as I mentioned previously, is to keep the header of
outer as clutter free as possible. This would be desirable because of at
least three reasons:

i) outer itself is well in excess of 150 lines of code and comments
ii) outer nests several helper classes (delegates)
iii) Each of these helper (i.e. nested) classes themselves are
potentially about 100+ loc or more (each) in their respective headers.

So the remaining question is:

Do I have no choice other than to expose the _entire_ API of a nested
class to the outer class?

I look forward to your response. MTIA
 
A

Alfonso Morra

vindhya said:
Modify your code like this.


class outer {
public:
outer() ;
~outer() ;
private:
class inner ;
class inner{public: inner(); ~inner(); void dothis(); void
dothat();};
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"

outer::inner::inner() ;
~outer::inner::inner() ;
void outer::inner::dothis( void ) {} ;
void outer::inner::dothat( void ){} ;

}


The reason for this is :
1) Multiple looping is happening because of recursivenes in inclusion
of header files.
2)When you create an object from a class then you need to declare and
define the class. The implementation can be left and done seperately.
3)As mentioned in above point you also need to define dothis and dothat
before using them. Compiler is unaware of it when you try to acces
inner::dothis.

Is there any special reason to code the way you have done or is it just
for testing purpose.

MS

I notice your example has lost the friend relationship between outer and
inner. was this intentional or merely an oversight?. tkx
 
A

Alfonso Morra

vindhya said:
Modify your code like this.


class outer {
public:
outer() ;
~outer() ;
private:
class inner ;
class inner{public: inner(); ~inner(); void dothis(); void
dothat();};
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"

outer::inner::inner() ;
~outer::inner::inner() ;
void outer::inner::dothis( void ) {} ;
void outer::inner::dothat( void ){} ;

}


The reason for this is :
1) Multiple looping is happening because of recursivenes in inclusion
of header files.
2)When you create an object from a class then you need to declare and
define the class. The implementation can be left and done seperately.
3)As mentioned in above point you also need to define dothis and dothat
before using them. Compiler is unaware of it when you try to acces
inner::dothis.

Is there any special reason to code the way you have done or is it just
for testing purpose.

MS

I notice your example has lost the friend relationship between outer and
inner. Was this intentional or merely an oversight?
 
V

vindhya

Oops I missed it. Anyways it has nothing to do with our problems.
What I think is that if we can seperate definition and implementaion
then we can achieve it. Have a look at the code below.

#include <iostream>

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



};

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

void outer::inner::dothis( void ) {} ;
void outer::inner::dothat( void ){} ;
void outer::dothis(void) { m_inner->dothis(); };
void outer::dothat(void) { m_inner->dothat(); };
 
A

Alfonso Morra

vindhya said:
Oops I missed it. Anyways it has nothing to do with our problems.
What I think is that if we can seperate definition and implementaion
then we can achieve it. Have a look at the code below.

#include <iostream>

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



};

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

void outer::inner::dothis( void ) {} ;
void outer::inner::dothat( void ){} ;
void outer::dothis(void) { m_inner->dothis(); };
void outer::dothat(void) { m_inner->dothat(); };

This looks like a (nicer) solution - as it allows for a cleaner
partitionng between the classes. I'll play around for a while with this
and see where it leads ... tkx
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top