1

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?

7
  • Having a virtual container hierarchy in C++ is highly unorthodox. Normally, this would be done completely different so I’m surprised that this is a University class assignment. It seems to teach a very weird and impractical C++ style. Oct 27, 2010 at 6:54
  • 1
    If this is what the C++ class is going to teach you, transfer to another one if possible. If not, please talk to the professor about his teaching style (difficult to do, though), and maybe try not to get too dinged up by the class.
    – GManNickG
    Oct 27, 2010 at 7:03
  • How would this be done completely different? Why is this so bad? there is alot more code that i did not show you. Like a wrapper class.association,object class etc.. So, the Object class hierarchy in c++ is something i shouldnt know? I still need to add all the link list queue, stack, etc..
    – user249375
    Oct 27, 2010 at 7:57
  • Isnt this just Data Structures and Algorithms with Object-Oriented Design Patterns in C++? Why is abstract classes, objects, container, so bad? i am just trying to understand.
    – user249375
    Oct 27, 2010 at 7:59
  • @icelated: There is no "Object class hierarchy" in C++.
    – Roger Pate
    Oct 27, 2010 at 9:56

3 Answers 3

2

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

1

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.