473,473 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

assign const_iterator to const pointer

Hi,

I'm having trouble with something that would appear to have a simple
solution.

Here's a version of the code I'm working with:
const Item* p 0;
name::const_iterator i;
name:const_iterator e = p1->end();
for (i=p1->begin(); i<e ++i)
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}

The compiler returns that it "cannot convert from const_iterator to
const(Item*)"
I have spent a few hours looking for a definitive answer and have found
nothing so far.

Any help will be greatly appreciated.

Cheers.

Patrick

Nov 9 '06 #1
4 2698

kotau wrote:
Hi,

I'm having trouble with something that would appear to have a simple
solution.

Here's a version of the code I'm working with:
const Item* p 0;
What is Item?
name::const_iterator i;
What is name?
name:const_iterator e = p1->end();
Missing a :
What is p1?
for (i=p1->begin(); i<e ++i)
iterators should be compared with operator == and operator !=, not with
operator <
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}

The compiler returns that it "cannot convert from const_iterator to
const(Item*)"
Because it probably can't and why should it? Iterators do not have to
take assignment to pointers.
I have spent a few hours looking for a definitive answer and have found
nothing so far.
The answer lies not in how you get your iterator to assign to a pointer
but to how to manage to do what you need to do without it.

Nov 9 '06 #2
Hi,

Thanks for your reply. Sorry for the syntax errors above.

Here's a more complete version:

static const Item* somefunc(const name* p1, const char* var)
{

const Item* p = 0;
name::const_iterator i;
name::const_iterator e = p1->end();
for (i=p1->begin(); i<e ++i)
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}
}
Someone else wrote the code and I'm trying to fix it...

Cheers.

On Nov 9, 6:00 pm, "Earl Purple" <earlpur...@gmail.comwrote:
kotau wrote:
Hi,
I'm having trouble with something that would appear to have a simple
solution.
Here's a version of the code I'm working with:
const Item* p 0;What is Item?
name::const_iterator i;What is name?
name:const_iterator e = p1->end();Missing a :
What is p1?
for (i=p1->begin(); i<e ++i)iterators should be compared with operator == and operator !=, not with
operator <
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}
The compiler returns that it "cannot convert from const_iterator to
const(Item*)"Because it probably can't and why should it? Iterators do not have to
take assignment to pointers.
I have spent a few hours looking for a definitive answer and have found
nothing so far.The answer lies not in how you get your iterator to assign to a pointer
but to how to manage to do what you need to do without it.
Nov 9 '06 #3

"kotau" <dj********@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
Hi,

Thanks for your reply. Sorry for the syntax errors above.

Here's a more complete version:

static const Item* somefunc(const name* p1, const char* var)
{

const Item* p = 0;
name::const_iterator i;
name::const_iterator e = p1->end();
for (i=p1->begin(); i<e ++i)
{
if (stricmp(i->func(), var) == 0)
{
p = i;
p is an Item*, i is a const_iterator. They are not assignable. The way to
convert an iterator to a pointer is to dereference the iterator, then take
the address of it. Try:
p = &(*i);
break;
}
}
}
Someone else wrote the code and I'm trying to fix it...

Cheers.

On Nov 9, 6:00 pm, "Earl Purple" <earlpur...@gmail.comwrote:
>kotau wrote:
Hi,
I'm having trouble with something that would appear to have a simple
solution.
Here's a version of the code I'm working with:
const Item* p 0;What is Item?
name::const_iterator i;What is name?
name:const_iterator e = p1->end();Missing a :
What is p1?
for (i=p1->begin(); i<e ++i)iterators should be compared with
operator == and operator !=, not with
operator <
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}
The compiler returns that it "cannot convert from const_iterator to
const(Item*)"Because it probably can't and why should it? Iterators do
not have to
take assignment to pointers.
I have spent a few hours looking for a definitive answer and have found
nothing so far.The answer lies not in how you get your iterator to
assign to a pointer
but to how to manage to do what you need to do without it.

Nov 9 '06 #4
Thanks that gets the compile working.

This group rocks!!!

Patrick.

On Nov 9, 6:17 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"kotau" <djcjeve...@gmail.comwrote in messagenews:11**********************@h54g2000cwb.g ooglegroups.com...
Hi,
Thanks for your reply. Sorry for the syntax errors above.
Here's a more complete version:
static const Item* somefunc(const name* p1, const char* var)
{
const Item* p = 0;
name::const_iterator i;
name::const_iterator e = p1->end();
for (i=p1->begin(); i<e ++i)
{
if (stricmp(i->func(), var) == 0)
{
p = i;p is an Item*, i is a const_iterator. They are not assignable. The way to
convert an iterator to a pointer is to dereference the iterator, then take
the address of it. Try:
p = &(*i);
break;
}
}
}
Someone else wrote the code and I'm trying to fix it...
Cheers.
On Nov 9, 6:00 pm, "Earl Purple" <earlpur...@gmail.comwrote:
kotau wrote:
Hi,
I'm having trouble with something that would appear to have a simple
solution.
Here's a version of the code I'm working with:
const Item* p 0;What is Item?
name::const_iterator i;What is name?
name:const_iterator e = p1->end();Missing a :
What is p1?
for (i=p1->begin(); i<e ++i)iterators should be compared with
operator == and operator !=, not with
operator <
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}
The compiler returns that it "cannot convert from const_iterator to
const(Item*)"Because it probably can't and why should it? Iterators do
not have to
take assignment to pointers.
I have spent a few hours looking for a definitive answer and have found
nothing so far.The answer lies not in how you get your iterator to
assign to a pointer
but to how to manage to do what you need to do without it.
Nov 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: john smith | last post by:
Hi, I'm a little confused as to why the following code generates and error when compiling: #include <iostream> #include <vector> #include <iterator> using namespace std; void...
3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
4
by: Gernot Frisch | last post by:
void Class::work() const { for(const std::list<int>::const_iterator i = m_pts.begin(); i!=m_pts.end(); ++i); } cannot find binary operator ++ that accepts left side type of const...
2
by: PengYu.UT | last post by:
I'm wonder whether 1. stl directly defined the 6 comparison operators(== != < > <= >=) directly for iterator and const_iterator 2. or it only define == and < and using std::rel_ops to get the...
5
by: John Harrison | last post by:
I there a reliable and generic method to convert a const_iterator to an iterator (i.e. something like const_cast). I ask because I'm writing some methods which take and return iterators. A const...
7
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that...
1
by: flopbucket | last post by:
Hi, For the learning experience, I am building a replacement for std::map. I built a templated red-black tree, etc., and all the basic stuff is working well. I implemented basic iterators and...
10
by: Yahooooooooo | last post by:
Hi, learning c++, can some one check below program. its giving compiler error. #include <iostream> #include <list> using namespace std; typedef list<intListInt;
2
by: subramanian100in | last post by:
Consider the code fragment: vector<intcontainer; container.push_back(0); container.push_back(1); container.push_back(2); Now I want to iterate through the 'container'. For this, instead...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.