English 中文(简体)
Custom container requirement to work with Qt s foreach
原标题:

What is the bare minimum amount of code to create a custom container that would work with Qt foreach macro?

I have this so far

template< class T >
class MyList
{
public:
  class iterator
  {
  public:

  };
  class const_iterator
  {
  public:
    inline iterator& operator++ ()
    {
      return *this;
    }
  };
};

and I m getting this compiler error:

4>.main.cpp(42) : error C2100: illegal indirection
4>.main.cpp(42) : error C2440:  initializing  : cannot convert from  MyList<T>::const_iterator  to  int 
4>        with
4>        [
4>            T=int
4>        ]
4>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

when I try to compile this:

  MyList<int> mylst;
  foreach(int num, mylst )
    qDebug() << num;
最佳回答

I ve omitted the dummy implementations I used but this compiled for me:

template< class T >
class MyList
{
public:
    class const_iterator
    {
    public:
        const T& operator*();
        bool operator!=( const const_iterator& ) const;
        const_iterator& operator++();
    };

    const_iterator begin() const;
    const_iterator end() const;
};
问题回答

As a disclaimer I am not sure if this is possible.

Check out the definition of foreach in qglobal.h. It looks like you may need to define a begin and end methods.

On my system it is found at $QtInstallDir/src/corelib/global/qglobal.h





相关问题
Silverlight: Add same UserControl to more than one Canvas

I have a bunch of UserControls ( MyUserControl ) that I want to have a user manually add to one or more Canvases. One instance of a UserControl cannot be a child element of more than one container (...

c++ templated container scanner

here s today s dilemma: suppose I ve class A{ public: virtual void doit() = 0; } then various subclasses of A, all implementing their good doit method. Now suppose I want to write a function ...

Multi-Dimensional Container in Java

I searched around on Google, but I was unable to find any libraries for a multi-dimensional container in Java (preferably one that supports generics as well). I could easily write one (in fact, I ...

c# stack queue combination

is there in C# some already defined generic container which can be used as Stack and as Queue at the same time? I just want to be able to append elements either to the end, or to the front of the ...

Custom container requirement to work with Qt s foreach

What is the bare minimum amount of code to create a custom container that would work with Qt foreach macro? I have this so far template< class T > class MyList { public: class iterator { ...

Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

热门标签