views:

56

answers:

3

This isnt a homework question. I took data structures at a Community College and now that i am at the university i talked to the teacher about there data structures class. Now, since its really different and the class i took transferred, He gave me one of there assignments and said play with it. We never did any containers, wrappers,templates,.. So, i am a little lost, and i am trying to get up to speed on this. I am not new to linklist,queue,stack,circular arrays,trees,etc.. We just never did any of that ADT hierarchy with object, container.

I do not have a book on ADT hierarchy - container, object.. Can anyone recommend one. not sure what to look up? just ADT?

Here is my problem. I am trying to complete this code he gave me.. I am trying to write the function the operator ++ () = 0; I am not sure about the syntax

#ifndef ITERATOR_H
#define ITERATOR_H
#include "Object.h"


class Iterator
{
public:
virtual ~Iterator ();
virtual void Reset () = 0;
virtual bool IsDone () const = 0;
virtual Object& operator * () const = 0;
virtual void operator ++ () = 0;
};

#endif

and here is the container header..

#ifndef CONTAINER_H
#define CONTAINER_H

#include <ostream>
#include "Object.h"
#include "Ownership.h"
#include "Iterator.h"
#include "Visitor.h"

class Container : public virtual Object, public virtual Ownership
{
protected:
unsigned int count;

Container ();
public:
virtual unsigned int Count () const;
virtual bool IsEmpty () const;
virtual bool IsFull () const;
//  virtual HashValue Hash () const;
virtual void Put (ostream&) const;
virtual Iterator& NewIterator () const;

virtual void Purge () = 0;
virtual void Accept (Visitor&) const = 0;
};

#endif

and here is the container.cpp file where i need help with the syntax for ++

#include <iostream>
#include "Container.h"

void Container::Purge()
{

if (IsOwner())

count = 0;

}

Container::Container () :
count (0)
{}

unsigned int Container::Count () const
{ return count; }

bool Container::IsEmpty () const
{ return Count () == 0; }

bool Container::IsFull () const
{ return false; }



Iterator Iterator::operator ++() //syntax wrong..
{

return;

}

Iterator& Container::NewIterator() const
{

return *new Container (*this); 


}

I am also not sure if i did the virtual Iterator& NewIterator () const; right?

A: 

I can see one likely problem to the 'syntax error' issue.

Your operator++ is declared to return a void, which does not match the return type in the definition.

Not reviewed the code for design aspects though...

Chubsdad
+2  A: 

C++ has iterator concepts which are subtly different from what you'll see in a pattern book or other languages. That he has this op++ return void is very non-C++; I would have made it a regular named method if keeping this model. However, that's your issue there: you have a return type of Iterator, while the class definition says void.

NewIterator is infinitely recursive (apparently quickly fixed? mispaste?), and the return type should be by-value, not by-reference. This return type is wrong in both the header and .cpp file. However, there's something else going on here, because Iterator is an abstract base class; I believe you're supposed to write your own derived class:

struct MyIterator : Iterator {
  //...
};

Actually, scratch that, Container is also an abstract base class, not something you should be implementing directly. (Which means the NewIterator either has a memory leak or only supports one iterator per container, so it's not really a new iterator...)

You need to ask for more information. You don't have enough to help us help you. I'd recommend Programming: Principles and Practice using C++ as a general introduction for C++ to new programmers, but it covers a wider spectrum than data structures and may not help you with the university prep you're doing now — it seems like they're trying to write Java in C++...

Roger Pate
A: 

I think you should have a look at the iterators in the STL. It will help you for your conception.

About your code, operator++ is declared to return void, which is wrong. You also forgot the other version of operator++, the operator-- and operator-> member functions which are great helpers to use iterators.

Your container has no member variable to actually store data and NewIterator has an infinite recursion in it.

You also lack a lot of references.

Opera