473,779 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1788
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::i nner() ;
~outer::inner:: inner() ;
void outer::inner::d othis( void ) {} ;
void outer::inner::d othat( 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<iostre am.h>

class outer {
public:
outer(){cout<<" Constructing outer object"<<endl;}
~outer(){cout<< "Destructin g outer object"<<endl;}
private:
class inner{
public:
inner(){cout<<" Constructing inner object"<<endl;}
~inner(){cout<< "Destructin g 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::i nner() ;
~outer::inner:: inner() ;
void outer::inner::d othis( void ) {} ;
void outer::inner::d othat( 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::i nner() ;
~outer::inner:: inner() ;
void outer::inner::d othis( void ) {} ;
void outer::inner::d othat( 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::i nner() ;
~outer::inner:: inner() ;
void outer::inner::d othis( void ) {} ;
void outer::inner::d othat( 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::d othis( void ) {} ;
void outer::inner::d othat( void ){} ;
void outer::dothis(v oid) { m_inner->dothis(); };
void outer::dothat(v oid) { 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::d othis( void ) {} ;
void outer::inner::d othat( void ){} ;
void outer::dothis(v oid) { m_inner->dothis(); };
void outer::dothat(v oid) { 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
29636
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 were still an option. However, I am coming up dry looking for information on how to do this, and the most recent references I am able to find to nested classes in PHP are dated 2003. And they all say the same thing: sorry, can't do that.
2
7811
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 lot of repeats and the concatenation field date always comes back as January 2003 instead of the month and date its supposed to --Fiscal Year
9
1540
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
9479
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. In a clean design, these "satelite" objects (i.e. declared in the satellite header files), should really be nested classes within the master file. However, if I was to put all the declarations in one massive file, it will easily exceed 1000...
12
4244
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 yo use? Do you group members by visibility - and how does that relate to grouping by type? Where do you insert (nested) types such as enums and delegates in classes
2
1139
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 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":
28
72552
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 2005 Test Environments. What is the purpose of a view if I can just copy the vode from a view and put it into a stored procedure? Should I be accessing views from stored procedures?
4
3545
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 whole application (its small for testing purposes) and it seems that window wraps the text, at least when I preview this post: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
20
2473
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 doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.