ADT - 迭代器 - 运算符 ++

发布于 2024-09-29 11:03:40 字数 1807 浏览 8 评论 0原文

这不是家庭作业问题。我在社区学院学习了数据结构,现在我在大学里,我和老师谈论了数据结构课程。现在,由于情况确实不同,而且我转的课程,他给了我其中一项作业,并说玩它。我们从来没有做过任何容器、包装、模板…… 所以,我有点迷失,我正在努力加快步伐。我对链表、队列、堆栈、循环数组、树等并不陌生。我们只是从未对对象、容器做过任何 ADT 层次结构。

我没有一本关于 ADT 层次结构 - 容器、对象的书。任何人都可以推荐一本吗?不确定要查找什么?只是ADT?

这是我的问题。我正在尝试完成他给我的这段代码.. 我正在尝试编写函数operator ++ () = 0; 我不确定语法

#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

,这里是容器标头..

#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

这里是container.cpp 文件,我需要 ++ 语法的帮助,

#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); 


}

我也不确定我是否执行了虚拟迭代器& NewIterator() const;正确的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末染殇 2024-10-06 11:03:40

C++ 的迭代器概念与您在模式书或其他语言中看到的略有不同。他有这个 op++ return void 是非常非 C++ 的;如果保留此模型,我会将其设为常规命名方法。然而,这就是你的问题:你有一个 Iterator 的返回类型,而类定义为 void。

NewIterator 是无限递归的(显然很快就修复了?错误粘贴?),并且返回类型应该是按值,而不是按引用。这个返回类型在头文件和 .cpp 文件中都是错误的。然而,这里还发生了其他事情,因为 Iterator 是一个抽象基类;我相信您应该编写自己的派生类:

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

实际上,从头开始,Container也是一个抽象基类,而不是您应该直接实现的东西。 (这意味着 NewIterator 要么存在内存泄漏,要么只支持每个容器一个迭代器,因此它并不是真正的迭代器...)

您需要询问更多信息。你没有足够的钱来帮助我们帮助你。我推荐编程:使用 C++ 的原理与实践作为对新程序员的 C++ 总体介绍,但它涵盖了比数据结构更广泛,可能对你现在正在做的大学预科没有帮助——看起来他们正在尝试用 C++ 编写 Java...

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

你穿错了嫁妆 2024-10-06 11:03:40

我认为你应该看看STL 中的迭代器。它会对您的受孕有所帮助。

关于您的代码,operator++ 被声明为返回 void,这是错误的。你还忘记了operator++的另一个版本,operator--和operator->。成员函数是使用迭代器的好帮手。

您的容器没有成员变量来实际存储数据,并且 NewIterator 中有无限递归。

你还缺乏很多参考资料。

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.

泅人 2024-10-06 11:03:40

我可以看到“语法错误”问题可能存在的一个问题。

您的运算符++被声明返回一个void,它与定义中的返回类型不匹配。

但没有审查设计方面的代码......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文