nested classes and use of 'friend'

The problem I have to resolve is as follows:
Create a Hen class. Inside this, nest a Nest class. Inside Nest, place an Egg class. Each class should have a display( ) member function. In main( ), create an instance of each class and call the display( ) function for each one.

I am using Borland Builder.
I have tried various alternative codes but the linker problem remains. To check the code I have blanked entries related to the third inner level.

I have searched in a large number of user groups sites for information on multi-level nested classes and could only find examples for one-level nested classes. Your help will be greatly appreciated.




#include <iostream>
#include <conio.h>
#include <vcl.h>

using namespace std;

class hen
{

public:
class nest;
friend class nest;
class egg;
friend class egg;
static int var_hen;
int fn_hen(nest a);
// int fn_hen_2(nest::egg b); this does not work
class nest
{
public:
static int var_nest;
void fn_nest(int nn){var_nest = nn;};
class egg
{
public:
static int var_egg;
void fn_egg(int ee){var_egg = ee;};

};
};
private:
};

int hen::fn_hen(nest a)
{
hen::var_hen = a.var_nest;
return var_hen;
}
//int hen::fn_hen_2(nest::egg b) this does not work
// {
// hen::var_hen = b.var_egg;
// return var_hen;
// }


int main()
{
hen aa;
hen::nest bb;
bb.fn_nest(500);
cout << "This is output for hen-nest " << aa.fn_hen(bb) << endl;
hen::nest::egg cc;
cc.fn_egg(700);
//cout << "This is output for hen-egg " << aa.fn_hen_2(cc) << endl;

}



Build
[Linker Error] Unresolved external 'hen::nest::var_nest' referenced from C:\STUDY_CPLUS\PROJ_THINKINGCPLUPLUS\PROJ_TCPP_CH05E\UNIT1.OBJ
[Linker Error] Unresolved external 'hen::var_hen' referenced from C:\STUDY_CPLUS\PROJ_THINKINGCPLUPLUS\PROJ_TCPP_CH05E\UNIT1.OBJ
[Linker Error] Unresolved external 'hen::nest::egg::var_egg' referenced from C:\STUDY_CPLUS\PROJ_THINKINGCPLUPLUS\PROJ_TCPP_CH05E\UNIT1.OBJ
Your problem has nothing to do with nested classes.

You have declared several static int variables in the various classes. These are only declaring the variables; these do not instantiate them. To fix this, you must explicitly instantiate them:

at file scope:

 
int hen::var_hen;


etc. for the others as well.
Many thanks to jsmith for suggestion given.
int hen::var_hen; works but I had to remove
static int var_hen; to deal with multiple declartion error message

The program works well for the hen::nest level but there it is still incorrect for the hen::nest::egg level. Suggestions on how to correct function fn_hen_2 would be greatly appreciated.



OUTPUT:
Output for hen-nest = 500
var_egg from fn_egg is = 700
var_egg is = 3771088
Output for hen-egg = 3771088




#include <iostream>
#include <conio.h>
#include <vcl.h>

using namespace std;

class hen
{

public:
class nest;
friend class nest;
class egg;
friend class egg;
// static int var_hen;
int hen::var_hen;
int fn_hen(nest a);
int fn_hen_2(nest ); //this does not work
class nest
{
public:
// static int var_nest;
int hen::nest::var_nest;
void fn_nest(int nn){var_nest = nn;};
class egg
{
public:
// static int var_egg;
int hen::nest::egg::var_egg;
void fn_egg(int ee){var_egg = ee;
cout << "var_egg from fn_egg is = "<< var_egg << endl;};

};
};
private:
};

int hen::fn_hen(nest a)
{
hen::var_hen = a.var_nest;
return var_hen;
}
int hen::fn_hen_2(nest ) // this does not work yet
{
nest::egg d;
cout << "var_egg is = "<< d.var_egg << endl;
hen::var_hen = d.var_egg;
return var_hen;
}


int main()
{
hen aa;
hen::nest bb;
bb.fn_nest(500);
cout << "Output for hen-nest = " << aa.fn_hen(bb) << endl;
hen::nest::egg cc;
cc.fn_egg(700);
cout << "Output for hen-egg = " << aa.fn_hen_2(bb) << endl; // this does not work yet

}

I'm confused about your static variable. static means you want only one copy of the variable no matter how many class instantiations you have; they all share the same one. Not making it static means you want one copy of the variable per instantiation. Either you want one or the other; I don't know which.

What you changed above looks wrong, and indeed won't even compile on newer compilers.

You have to move the declaration of the nest class above fn_hen.
Many thanks to jsmith for the most helpful comments. I will consider upgrading my compiler.
Topic archived. No new replies allowed.