English 中文(简体)
它没有界定其对应的中间商的主子
原标题:It fails to define a const_iterator from it s counterpart iterator

我将装有自定集装箱的 st集装箱延伸,以便更灵活地控制部件的运行。

class MyContainer;

template <typename T> class myiterator :public iterator<bidirectional_iterator_tag, T>
{
  friend class MyContainer;
  private:
    T *pointer;

    myiterator(T *pt):pointer(pt) {}

  public:
    T& operator*() {return (*pointer);}

    const myiterator<T>& operator++()
    {
      pointer->current_iterator++;
       return *this;
    }

    bool isEnd(void) const
    {
      return pointer->current_iterator == pointer->data.end();
    }
  };

class MyContainer
{
  friend class myiterator<MyContainer>;
  public:
    typedef myiterator<MyContainer> iterator;
    typedef myiterator<MyContainer const> const_iterator;

  private:
    map<int, int> data;
    map<int, int>::const_iterator current_iterator;

  public:
    MyContainer() {current_iterator = data.begin(); }

    void addDataPair(int key, int value) {data[key] = value;}

    int first() const {return (*current_iterator).first;}
    int second() const {return (*current_iterator).second;}

    iterator begin() 
    {
      current_iterator = data.begin();
      return iterator(this);
    }

    const_iterator begin() const
    {
      return const_iterator(this);
    }
  };

如果我使用激光器作为跟踪器,则该代码行,。

MyContainer h;

h.addDataPair(1, 1);
h.addDataPair(2, 2);
h.addDataPair(3, 3);

for (MyContainer::iterator it=h.begin(); !it.isEnd(); ++it)
{
  cout << (*it).first() << " " << (*it).second() << endl;
}

但是,如果我改变召集人,它就赢得了汇编。 我读过一些条款,其中提到,为了确定固定的炉子,我们只是要取代从X到X星等值,这是我在守则中所做的。 但是,我很快发现,在我的案件中,它可能赢得一些工作,因为在我的案件中,一个炉子的回击是集装箱本身。 我当时不知道如何使召集人的工作不重复编码。

In addition, my iterator is derived from std::iterator but I found that I cannot override the constructor for my iterator. Is that any way that I can pass more than one parameters into my iterator besides the T *pt ? Thanks.

问题回答

第一问题:

如果你改变这一状况:

for (MyContainer::itera页: 1r it=h.begin(); !it.isEnd(); ++it)

页: 1

for (MyContainer::const_itera页: 1r it=h.begin(); !it.isEnd(); ++it)

then you get a non-const itera页: 1r from begin() and end() and try 页: 1 initialize a const_itera页: 1r from it, but that s a different type and your my_itera页: 1r template doesn t have a construc页: 1r that allows construction from a different type.

可以通过增加:

template<typename> friend class myitera页: 1r;

template<typename T2>
  myitera页: 1r(myitera页: 1r<T2> const& i) : pointer(i.pointer) { }

You should also make opera页: 1r* const (it doesn t alter the itera页: 1r 页: 1 dereference it.)

But there s still a bigger problem, a const_itera页: 1r points 页: 1 a const MyContainer, but const_itera页: 1r::opera页: 1r++ needs 页: 1 alter that object, which it can t because it s const. So you can t increment your const_itera页: 1r i.e. can t use it 页: 1 iterate! You might want 页: 1 rethink that design.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签