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_)
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_)