Incomplete types, "Cons" lists, inner classes

A

andrew

Hi,

I'm a C++ newbie, so apologies for this rather basic question. I've
searched for help and, while I understand the problem (that the outer
class is not yet defined), I don't understand what the "correct"
solution would be.

I am trying to model lists much like in Lisp, using a "Cons" object to
hold two pointers - one to an Element, and the other to a further Cons.
Cons is a subclass of Element, so lists can be nested.

A fragment of the class diagram is shown at
http://www.acooke.org/andrew/diary/2005/jun/cons.png

The Null class is used to terminate a list - it's the Cons used when
there are no more Cons's in the chain.

Now it seems to me that it's natural to put the Null class inside the
Cons class, since it is only used internally (when the single arg
constructor is used). But then I end up with code that is trying to
subclass Cons within Cons itself, and so have an incomplete type error.
The source is below.

What should I be doing? (I realise there are other ways of
implementing lists, including templates, but assume for the moment that
what I am trying to do is valid in broad terms).

Thanks!
Andrew

///////////////////////////////////////////////////////////
// Cons.h
// Implementation of the Class Cons
// Created on: 04-Jun-2005 13:38:21
///////////////////////////////////////////////////////////

#if !defined(EA_3C5191FE_5445_4ae0_87F5_DCA6FE052EC3__INCLUDED_)
#define EA_3C5191FE_5445_4ae0_87F5_DCA6FE052EC3__INCLUDED_

#include "Element.h"

namespace Element
{
class Cons : public Element
{

public:
Cons();
virtual ~Cons();
Cons(Element& element, Cons& cons);
Cons(Element& element);
Element head();
bool isEqual(Element& other);
Cons tail();

private:
class Null : public Cons
{

public:
Null();
virtual ~Null();
Element head();
bool isEqual(Element& other);
Cons tail();

};
Element *head;
Cons *tail;

};

}
#endif // !defined(EA_3C5191FE_5445_4ae0_87F5_DCA6FE052EC3__INCLUDED_)
 
A

andrew

More succinctly, can a nested class extend the class it is nested
within? If so, how do I write the header file to avoid "incomplete
type" errors?
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top