Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 925
sivabudh Avatar asked Nov 16 '09 21:11

sivabudh


1 Answers

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;
};
like image 104
Troubadour Avatar answered Oct 13 '22 20:10

Troubadour