C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

cooperating & nested classes

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Vasko Altiparmakov
Guest





PostPosted: Mon Mar 08, 2004 12:12 am    Post subject: cooperating & nested classes Reply with quote



greetings

i am trying to implement a stack class. i am mostly learning from a
book called "Practical Data Structures" in C++ by Bryan Flamig.
this is not a compiler specific question but the term forces me to
tell you what i use to compile. as sugested in the book, i was using
cooperating classes to do the job and everything worked fine, but then
at the end of the text it was mentioned that the same thing can be
done with nested classes too. it explained very little, just an
example of how the syntax should look so i have tried. here is the
definition of my cooperating classes:

class elem
{
friend stak;
public:
elem ();
elem (char, elem *);
~elem ();
private:
elem (elem &);
char data;
elem *next;
};

class stak
{
public:
stak ();
stak (char*);
stak (stak &);
~stak ();
void operator= (stak);
char pull (void);
void push (char);
void write (void);
stak * retaddress (void);
private:
void soisclear ();
void novacopy (const stak &);
int zbroj;
elem *prv;
};

this compiles under Borland Turbo C/C++ 1.01 with no errors and works
fine.
but when i try to do it with nested classes:

class stak
{
protected:
class elem
{
public:
elem ();
elem (char, elem *);
~elem ();
char data;
elem *next;
private:
elem (elem &);
};
public:
stak ();
stak (char*);
stak (stak &);
~stak ();
void operator= (stak);
char pull (void);
void push (char);
void write (void);
stak * retaddress (void);
private:
void soisclear ();
void novacopy (const stak &);
int zbroj;
elem *prv;
};

....

stak::elem::elem () // line 138
{
cout << endl << "default constructor... " << this << endl;
data = '?';
next = 0;
}

stak::elem::elem (char c, elem *rhs) // line 145 (i never use this,
since all the work is done in stak::stak (char *);
{
cout << endl << "general constructor..." << endl;
data = c;
next = rhs;
}

stak::elem::~elem () // line 152
{
cout << endl << data << " destructor..." << this << endl;
}

Turbo C++ Version 1.01 Copyright (c) 1990 Borland International
nstack.cpp:
Error nstack.cpp 138: Multiple scope qualifiers
Error nstack.cpp 145: Multiple scope qualifiers
Error nstack.cpp 152: Multiple scope qualifiers
*** 3 errors in Compile ***
Available memory 278304


it generates this 3 errors that i cannot understand and remove.
next i tried to give it a try with another compiler (dev c++ 4) which
generated "only" 28 errors on the source useing cooperating classes.
leaving only 5 errors if i move the stak class infront of the elem
class.

1. syntax error before `*'
2,3,4. `prv' undeclared (first use this function)
5. `class stak' has no member declared 'prv'


when i compile the nested class source (with dev cpp), on my surprise
works fine.
no complains.

my interest in this is not to make the .exe file and the whole thing
to work, but how should i make the source to be a good C++ one?
at the moment i don't have member functions to access privete member:
data, *next and *prv but no compiler complained.

thaks in advance
Vasko
Back to top
Claudio Puviani
Guest





PostPosted: Mon Mar 08, 2004 12:18 am    Post subject: Re: cooperating & nested classes Reply with quote



"Vasko Altiparmakov" <spacerogue5 (AT) yahoo (DOT) com> wrote
Quote:
greetings

i am trying to implement a stack class. i am mostly learning from a
book called "Practical Data Structures" in C++ by Bryan Flamig.
this is not a compiler specific question but the term forces me to
tell you what i use to compile. as sugested in the book, i was using
cooperating classes to do the job and everything worked fine, but then
at the end of the text it was mentioned that the same thing can be
done with nested classes too. it explained very little, just an
example of how the syntax should look so i have tried. here is the
definition of my cooperating classes:

class elem
{
friend stak;
public:
elem ();
elem (char, elem *);
~elem ();
private:
elem (elem &);
char data;
elem *next;
};

class stak
{
public:
stak ();
stak (char*);
stak (stak &);
~stak ();
void operator= (stak);
char pull (void);
void push (char);
void write (void);
stak * retaddress (void);
private:
void soisclear ();
void novacopy (const stak &);
int zbroj;
elem *prv;
};

this compiles under Borland Turbo C/C++ 1.01 with no errors and works
fine.
but when i try to do it with nested classes:

class stak
{
protected:
class elem
{
public:
elem ();
elem (char, elem *);
~elem ();
char data;
elem *next;
private:
elem (elem &);
};
public:
stak ();
stak (char*);
stak (stak &);
~stak ();
void operator= (stak);
char pull (void);
void push (char);
void write (void);
stak * retaddress (void);
private:
void soisclear ();
void novacopy (const stak &);
int zbroj;
elem *prv;
};

...

stak::elem::elem () // line 138
{
cout << endl << "default constructor... " << this << endl;
data = '?';
next = 0;
}

stak::elem::elem (char c, elem *rhs) // line 145 (i never use this,
since all the work is done in stak::stak (char *);
{
cout << endl << "general constructor..." << endl;
data = c;
next = rhs;
}

stak::elem::~elem () // line 152
{
cout << endl << data << " destructor..." << this << endl;
}

Turbo C++ Version 1.01 Copyright (c) 1990 Borland International
nstack.cpp:
Error nstack.cpp 138: Multiple scope qualifiers
Error nstack.cpp 145: Multiple scope qualifiers
Error nstack.cpp 152: Multiple scope qualifiers
*** 3 errors in Compile ***
Available memory 278304


it generates this 3 errors that i cannot understand and remove.
next i tried to give it a try with another compiler (dev c++ 4) which
generated "only" 28 errors on the source useing cooperating classes.
leaving only 5 errors if i move the stak class infront of the elem
class.

1. syntax error before `*'
2,3,4. `prv' undeclared (first use this function)
5. `class stak' has no member declared 'prv'


when i compile the nested class source (with dev cpp), on my surprise
works fine.
no complains.

my interest in this is not to make the .exe file and the whole thing
to work, but how should i make the source to be a good C++ one?
at the moment i don't have member functions to access privete member:
data, *next and *prv but no compiler complained.

thaks in advance
Vasko

Unfortunately for you, Turbo C++ 1.x is severely antiquated. Besides nested
classes, it doesn't support templates, namespaces, exceptions, and probably a
host of other features we take for granted today. You should probably give up on
Turbo C++.

Claudio Puviani



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.