By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,050 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,050 IT Pros & Developers. It's quick & easy.

Incomplete types, "Cons" lists, inner classes

P: n/a

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

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


// 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

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

class Null : public Cons

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

Element *head;
Cons *tail;


#endif // !defined(EA_3C5191FE_5445_4ae0_87F5_DCA6FE052EC3__ INCLUDED_)

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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?

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.