473,287 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Whats wrong with this code? (nested classes question)

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

Jul 23 '05 #1
7 1757
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

Jul 23 '05 #2
#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...

Jul 23 '05 #3


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

Jul 23 '05 #4


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

Jul 23 '05 #5


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?

Jul 23 '05 #6
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(); };

Jul 23 '05 #7


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

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
2
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a...
9
by: John Harrison | last post by:
Both gcc 3.3.1 and VC++ 7.1 compile the following code. struct Outer { struct Inner { int f() { return c; } }; private: static const int c;
2
by: Alfonso Morra | last post by:
I have a (largish) "master" header file (>130 loc). I also have a number of seperate "satelite" header files which declare various objects used by the object declared in the "master" header file....
12
by: Gerrit Beuze | last post by:
Hi all, I'm wondering if you how you organize as (in sorting / order) your C# class code: Do you sort/ group by member type: fields, methods, properties etc.? If yes: what ordering scheme do...
2
by: Alfonso Morra | last post by:
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...
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
4
by: blah | last post by:
Hello everyone, Ive been trying to get my application to "click" on a button in another application using SendMessage, Ive gotten this far but Im not sure whats wrong with this code, here is the...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.