Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strange problem with map iterator

Reply
Thread Tools

strange problem with map iterator

 
 
vivicrow
Guest
Posts: n/a
 
      08-31-2006
Hi, assume that I have a class called ClassA, if I do the following, it
works fine:
ClassA *p1 = new ClassA();
p1->Update();

if I do the following,
map<string, ClassA> myMap;
string name("something");
myMap.insert(pair<string,ClassA>(name, ClssA()));
map<stirng, ClassA>::iterator p;
p = botsAI.find(name);
p->second.Update();

I would get mystery errors
pure virtual method called
terminate called without an active exception

so my question is why calling the function through an iterator doesn't
work....what is the difference between the iterator and a normal
pointer???

Any help would be great!!!!!!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-31-2006
vivicrow wrote:
> Hi, assume that I have a class called ClassA, if I do the following,
> it works fine:
> ClassA *p1 = new ClassA();
> p1->Update();
>
> if I do the following,
> map<string, ClassA> myMap;
> string name("something");
> myMap.insert(pair<string,ClassA>(name, ClssA()));


You mean

myMap.insert(make_pair(name, CLassA()));

> map<stirng, ClassA>::iterator p;
> p = botsAI.find(name);


You mean

p = myMap.find(name);

> p->second.Update();


You never check whether it finds your pair or not. Are you sure it is
there? Do

if (p != myMap.end())

>
> I would get mystery errors
> pure virtual method called
> terminate called without an active exception
>
> so my question is why calling the function through an iterator doesn't
> work....what is the difference between the iterator and a normal
> pointer???


Plenty.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-31-2006
Victor Bazarov wrote:

>> if I do the following,
>> map<string, ClassA> myMap;
>> string name("something");
>> myMap.insert(pair<string,ClassA>(name, ClssA()));

>
> You mean
>
> myMap.insert(make_pair(name, CLassA()));


I think he rather means:

myMap.insert(make_pair(name, ClassA()));

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-31-2006
Rolf Magnus wrote:
> Victor Bazarov wrote:
>
>>> if I do the following,
>>> map<string, ClassA> myMap;
>>> string name("something");
>>> myMap.insert(pair<string,ClassA>(name, ClssA()));

>>
>> You mean
>>
>> myMap.insert(make_pair(name, CLassA()));

>
> I think he rather means:
>
> myMap.insert(make_pair(name, ClassA()));


THanks! ))


 
Reply With Quote
 
vivicrow
Guest
Posts: n/a
 
      08-31-2006
Victor Bazarov wrote:
> You mean
>
> myMap.insert(make_pair(name, CLassA()));
>


well, I think they are more or less the samething... but just tried
'make_pair', gave the same errors...

> > map<stirng, ClassA>::iterator p;
> > p = botsAI.find(name);

>
> You mean
>
> p = myMap.find(name);
>


you are right, my typo!!

> > p->second.Update();

>
> You never check whether it finds your pair or not. Are you sure it is
> there? Do
>
> if (p != myMap.end())
>

I didn't check because I am 100% sure that it finds the pair - I can do

cout << p->second.getName() << endl; //getName() returns
a string...
without problems....

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-31-2006
vivicrow wrote:
> Victor Bazarov wrote:
>> You mean
>>
>> myMap.insert(make_pair(name, CLassA()));
>>

>
> well, I think they are more or less the samething... but just tried
> 'make_pair', gave the same errors...
>
>>> map<stirng, ClassA>::iterator p;
>>> p = botsAI.find(name);

>>
>> You mean
>>
>> p = myMap.find(name);
>>

>
> you are right, my typo!!
>
>>> p->second.Update();

>>
>> You never check whether it finds your pair or not. Are you sure it
>> is there? Do
>>
>> if (p != myMap.end())
>>

> I didn't check because I am 100% sure that it finds the pair - I can
> do
>
> cout << p->second.getName() << endl; //getName() returns
> a string...
> without problems....


Well, this falls under FAQ 5.8, I guess. Read it and follow its
suggestions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
sillyemperor
Guest
Posts: n/a
 
      09-01-2006
Which compiler you use.
this is my code:
#include <map>
using std::map;

class A
{
public:
void f(){}
};

typedef map<int,A> AMap;

int main()
{
AMap m;
m[0] = A();

AMap::iterator i = m.find(0);

i->second.f();
}

there is no problem

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
will an iterator to a map becomes invalid when an element is inserted into the map wolverine C++ 3 07-31-2006 12:24 PM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments