strange problem with map iterator

V

vivicrow

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!!!!!!
 
V

Victor Bazarov

vivicrow said:
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
 
V

vivicrow

Victor said:
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...
You mean

p = myMap.find(name);

you are right, my typo!!
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....
 
V

Victor Bazarov

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


you are right, my typo!!

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
 
S

sillyemperor

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top