Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Whats wrong with this code? (nested classes question)

Reply
Thread Tools

Whats wrong with this code? (nested classes question)

 
 
Alfonso Morra
Guest
Posts: n/a
 
      07-19-2005
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

 
Reply With Quote
 
 
 
 
vindhya
Guest
Posts: n/a
 
      07-19-2005
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

 
Reply With Quote
 
 
 
 
sugary.boy@gmail.com
Guest
Posts: n/a
 
      07-19-2005
#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...

 
Reply With Quote
 
Alfonso Morra
Guest
Posts: n/a
 
      07-19-2005


vindhya wrote:

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

>
> MS
>


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

 
Reply With Quote
 
Alfonso Morra
Guest
Posts: n/a
 
      07-19-2005


vindhya wrote:

> 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

 
Reply With Quote
 
Alfonso Morra
Guest
Posts: n/a
 
      07-19-2005


vindhya wrote:

> 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?

 
Reply With Quote
 
vindhya
Guest
Posts: n/a
 
      07-19-2005
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(); };

 
Reply With Quote
 
Alfonso Morra
Guest
Posts: n/a
 
      07-19-2005


vindhya wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding static methods : Whats wrong with these classes?? ankur Java 3 08-10-2008 08:28 PM
Getting linking error: not found in vtable (Trying to understand whats wrong the way I did my classes?) g35rider@gmail.com C++ 1 08-17-2006 05:10 PM
I don't know whats wrong!!! =?Utf-8?B?S2lt?= Wireless Networking 2 09-08-2005 08:21 AM
Re: classes and list as parameter, whats wrong? Dirk Zimmermann Python 2 08-26-2005 04:36 PM
classes and list as parameter, whats wrong? Dirk Zimmermann Python 0 08-26-2005 09:34 AM



Advertisments