Boost logo

Boost Users :

From: gusz1_at_[hidden]
Date: 2004-05-21 03:56:54


Hi,
could please somebody tell me why the code below generates a compile-time
error?
The compiler I'm using is VC++ 7.1.

The error generated is this:

c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\algorithm(458) : error C2582: 'operator =' function is
unavailable in 'item_proxy'
        c:\PROG\VCPP\xItFacadeReferProxy\xItFacadeReferProxy.cpp(102) :
see reference to function template instantiation 'void
std::generate<array_iterator,int(__cdecl *)(void)>(_FwdIt,_FwdIt,_Fn0)'
being compiled
        with
        [
            _FwdIt=Array_iterator,
            _Fn0=int (__cdecl *)(void)
        ]

the location of the error in <algorithm>:

template<class _FwdIt,
        class _Fn0> inline
        void generate(_FwdIt _First, _FwdIt _Last, _Fn0 _Func)
        { // replace [_First, _Last) with _Func()
        for (; _First != _Last; ++_First)
                *_First = _Func(); // ERROR HERE
        }

To my best knowledge, item_proxy's operator= is public, so I don't
understand why it is unavailable.

Thx,
Gus

The code:

------------------------------------------------------------------------

// xItFacadeReferProxy.cpp : Defines the entry point
// for the console application.

#include "stdafx.h"
#include <algorithm>
#include <boost/iterator/iterator_facade.hpp>
using namespace boost;
using namespace std;

// THIS CLASS IS GOING TO BE 'STL'IZED':

const int ARRAY_SIZE = 10;

class Array {
public:
        int Get(int i) const { return data[i]; }
        int Set(int i, int val) { data[i] = val; }
private:
        int data[ARRAY_SIZE];
};//class Array

class item_proxy;

class Array_iterator
        : public iterator_facade
                <
                                Array_iterator,
                                int, // VALUE TYPE
                                boost::random_access_traversal_tag,
                                item_proxy // REFERENCE TYPE
>
{
public:

        class item_proxy {
        public:
                item_proxy(const Array_iterator &it):
                                        m_rIterator(it) {}

                item_proxy& operator=(int i) {
                                        m_rIterator.m_pArray->
                                                Set(m_rIterator.m_index,
i);
                                        return *this;
                }

                item_proxy& operator=(item_proxy const& rhs) {
                        operator=((int)rhs);
                        return *this;
                }

                operator int() const {
                                        return
                                                m_rIterator.m_pArray->
Get(m_rIterator.m_index);
                }

        private:
                Array_iterator const &m_rIterator;
        };//class item_proxy

public:
        Array_iterator():m_pArray(0) {}

        Array_iterator(Array& array, int index = 0):
                m_pArray(&array), m_index(0) {}

private:

        friend class boost::iterator_core_access;

                void increment() { ++m_index; }
        void decrement() { --m_index; }
        void advance(int n) { m_index += n; }

        int distance_to(Array_iterator const& rhs) {
                if(m_pArray != rhs.m_pArray) { return -1; }
                return rhs.m_index - m_index;
        }

        bool equal(Array_iterator const& rhs) const {
                return m_pArray == rhs.m_pArray &&
                                        m_index == rhs.m_index;
        }

        item_proxy dereference() const {
                return item_proxy(*this);
        }

        Array *m_pArray;
        int m_index;
};//class Array_iterator

Array_iterator array_begin(Array& array) {
        return Array_iterator(array);
}

Array_iterator array_end(Array& array) {
        return Array_iterator(array, ARRAY_SIZE);
}

int value_generator() {
        static int i = 0;
        return ++i;
}

int _tmain(int argc, _TCHAR* argv[])
{
        Array a;
        generate(array_begin(a), array_end(a), value_generator);
                        // ERROR!
        copy(array_begin(a), array_end(a),
                        ostream_iterator<int>(cout, " "));
        return 0;
}

------------------------------------------------------------------------


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net