C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
cppaddict
Guest





PostPosted: Fri Aug 27, 2004 11:38 pm    Post subject: Why can't I use an Iterator with a Reference Parameter? Reply with quote



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 Point,unsigned
long>,Point>,std::less<Point>,std::allocator<std::pair Point,unsigned lon
g> > >::const_iterator' to '__rwstd::__rb_tree<Point,std::pair Point,unsig
ned long>,__rwstd::__select1st<std::pair long>,Point>,std::
less<Point>,std::allocator<std::pair
Quote:
::iterator' i
n function NativeOcr::testPoints(const Point &,const

std::map<Point,unsigned lon
g,std::less
Quote:
&) co
nst

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

Back to top
Andrey Tarasevich
Guest





PostPosted: Fri Aug 27, 2004 11:51 pm    Post subject: Re: Why can't I use an Iterator with a Reference Parameter? Reply with quote



cppaddict wrote:

Quote:
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

Back to top
cppaddict
Guest





PostPosted: Fri Aug 27, 2004 11:57 pm    Post subject: Re: Why can't I use an Iterator with a Reference Parameter? Reply with quote




Quote:
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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.