473,796 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Incomplete types, "Cons" lists, inner classes

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_3C5 191FE_5445_4ae0 _87F5_DCA6FE052 EC3__INCLUDED_)
#define EA_3C5191FE_544 5_4ae0_87F5_DCA 6FE052EC3__INCL UDED_

#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_3C5 191FE_5445_4ae0 _87F5_DCA6FE052 EC3__INCLUDED_)

Jul 23 '05 #1
1 1497
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2328
by: Terje Slettebø | last post by:
Hi. I'm new here, and sorry if this has been discussed before; I didn't find it searching the PHP groups. (I've also read recommendations to cross-post to the other PHP groups, but if that is discouraged, please let me know. At the same time, please then let me know which of the many PHP groups to post to. :) ) In PHP5, you can provide "type hints" for functions, like this:
87
5264
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at what point when learning C++ was I supposed to have picked up STL? I ask because at every job I've had, and every job interview for that matter, whenever STL comes up it's not I, but a manager who says effectively yuck, we don't use that crap.
6
1808
by: Code4u | last post by:
I need to design data storage classes and operators for an image processing system that must support a range of basic data types of different lengths i.e. float, int, char, double. I have a template class that stores the data. The problem with this design is the inability to treat image data generically- I have a set of specialized classes with no connection. I'm considering deriving all ImageData<T> classes from a image data abstract base...
4
1703
by: Piotr Filip Mieszkowski | last post by:
Hello, I like both C++ and Lisp and sometimes try to mix their ideas in the code I write. Recently I started to think about writing a pair class similar to the CONS in Lisp. (For those of you who don't know Lisp: lists in Lisp are constructed of pairs whose second element is always the next pair or NIL, a special symbol.) So I've tried something like this: template <class A, class B>
175
8910
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
24
3839
by: Ronald S. Cook | last post by:
An ongoing philosophical argument, I would like your opinions. With the release of .NET, Microsoft spoke of moving away from the notation as a best practice. I'm a believer for a few reasons: 1) Consistency throughout and knowing which objects are yours (clsEmployee, tblEmployee, frmEmployee, etc). 2) Not having to name an employee form EmployeeForm.aspx because the mane is already taken by your class named Employee.cs
30
3857
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at the end of the app for example: class test { public: int x;
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.