Why can't I use an Iterator with a Reference Parameter?

Discussion in 'C++' started by cppaddict, Aug 28, 2004.

  1. cppaddict

    cppaddict Guest

    My initial method sig was:

    bool NativeOcr::testPoints(Point positionCoords, std::map<Point,
    COLORREF> testPnts) const ;

    I changed it to:

    bool NativeOcr::testPoints(const Point& positionCoords, const
    std::map<Point, COLORREF>& testPnts) const ;

    Whereas before it compiled, now it errors on the line marked below
    (excerpt from method body):

    std::map<Point, COLORREF>::iterator iter;
    int x,y;
    //ERROR ON NEXT LINE
    for (iter = testPnts.begin(); iter != testPnts.end(); ++iter) {
    //do stuff
    }

    I thought you could use refs just like the regular variable.
    According the FAQ at:

    http://www.parashift.com/c -faq-lite/references.html

    "please do not think of a reference as a funny looking pointer to an
    object. A reference is the object. It is not a pointer to the object,
    nor a copy of the object. It is the object."

    Why does this not apply here?

    Thanks for an explanation,
    cpp

    -----------------ERROR MESSAGE---------------
    Error E2034 NativeOcr.cpp 494: Cannot convert
    '__rwstd::__rb_tree<Point,std::pai
    r<const Point,unsigned long>,__rwstd::__select1st<std::pair<const
    Point,unsigned
    long>,Point>,std::less<Point>,std::allocator<std::pair<const
    Point,unsigned lon
    g> > >::const_iterator' to '__rwstd::__rb_tree<Point,std::pair<const
    Point,unsig
    ned long>,__rwstd::__select1st<std::pair<const Point,unsigned
    long>,Point>,std::
    less<Point>,std::allocator<std::pair<const Point,unsigned long> >
    >::iterator' i

    n function NativeOcr::testPoints(const Point &,const
    std::map<Point,unsigned lon
    g,std::less<Point>,std::allocator<std::pair<const Point,unsigned long>
    > > &) co

    nst
    Error E2094 NativeOcr.cpp 494: 'operator!=' not implemented in type
    '__rwstd::__
    rb_tree<Point,std::pair<const Point,unsigned
    long>,__rwstd::__select1st<std::pai
    r<const Point,unsigned
    long>,Point>,std::less<Point>,std::allocator<std::pair<co
    nst Point,unsigned long> > >::iterator' for arguments of type
    '__rwstd::__rb_tre
    e<Point,std::pair<const Point,unsigned
    long>,__rwstd::__select1st<std::pair<cons
    t Point,unsigned
    long>,Point>,std::less<Point>,std::allocator<std::pair<const Po
    int,unsigned long> > >::const_iterator' in function
    NativeOcr::testPoints(const
    Point &,const std::map<Point,unsigned
    long,std::less<Point>,std::allocator<std::
    pair<const Point,unsigned long> > > &) const
    *** 2 errors in Compile ***
    make: *** [NativeOcr.obj] Error 1
    cppaddict, Aug 28, 2004
    #1
    1. Advertising

  2. cppaddict wrote:

    > My initial method sig was:
    >
    > bool NativeOcr::testPoints(Point positionCoords, std::map<Point,
    > COLORREF> testPnts) const ;
    >
    > I changed it to:
    >
    > bool NativeOcr::testPoints(const Point& positionCoords, const
    > std::map<Point, COLORREF>& testPnts) const ;
    >
    > Whereas before it compiled, now it errors on the line marked below
    > (excerpt from method body):
    >
    > std::map<Point, COLORREF>::iterator iter;
    > int x,y;
    > //ERROR ON NEXT LINE
    > for (iter = testPnts.begin(); iter != testPnts.end(); ++iter) {
    > //do stuff
    > }
    >
    > I thought you could use refs just like the regular variable.


    In this case it is not about references, it is about const-correctness.
    You made your 'std::map<>' parameter const-qualified. For this reason in
    the reference-based version the container's 'begin()' method returns
    'std::map<>::const_iterator'. A 'const_iterator' cannot be assigned to
    (or used to initialize) an 'iterator'. That's what's causing the error.
    Change the iterator declaration to

    std::map<Point, COLORREF>::const_iterator iter;

    and the code should compile.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Aug 28, 2004
    #2
    1. Advertising

  3. cppaddict

    cppaddict Guest


    >In this case it is not about references, it is about const-correctness.
    >You made your 'std::map<>' parameter const-qualified. For this reason in
    >the reference-based version the container's 'begin()' method returns
    >'std::map<>::const_iterator'. A 'const_iterator' cannot be assigned to
    >(or used to initialize) an 'iterator'. That's what's causing the error.
    >Change the iterator declaration to
    >
    > std::map<Point, COLORREF>::const_iterator iter;
    >
    >and the code should compile.


    That was it. Thanks very much.

    cpp
    cppaddict, Aug 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hunter Hou
    Replies:
    5
    Views:
    423
    Jerry Coffin
    Jun 18, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    855
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,747
    Smokey Grindel
    Dec 2, 2006
  4. Pranav
    Replies:
    3
    Views:
    684
    ManicQin
    Aug 8, 2008
  5. Isti
    Replies:
    2
    Views:
    698
Loading...

Share This Page