cooperating & nested classes

  • Thread starter Vasko Altiparmakov
  • Start date
V

Vasko Altiparmakov

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
 
C

Claudio Puviani

Vasko Altiparmakov said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top