strange problem with map iterator

Discussion in 'C++' started by vivicrow, Aug 31, 2006.

  1. vivicrow

    vivicrow Guest

    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!!!!!!
     
    vivicrow, Aug 31, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 31, 2006
    #2
    1. Advertising

  3. vivicrow

    Rolf Magnus Guest

    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()));
     
    Rolf Magnus, Aug 31, 2006
    #3
  4. 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! :)))
     
    Victor Bazarov, Aug 31, 2006
    #4
  5. vivicrow

    vivicrow Guest

    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....
     
    vivicrow, Aug 31, 2006
    #5
  6. 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
     
    Victor Bazarov, Aug 31, 2006
    #6
  7. vivicrow

    sillyemperor Guest

    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
     
    sillyemperor, Sep 1, 2006
    #7
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,430
  2. greg
    Replies:
    6
    Views:
    460
    Dietmar Kuehl
    Jul 17, 2003
  3. wolverine
    Replies:
    3
    Views:
    1,079
    Chris
    Jul 31, 2006
  4. Vikram Karandikar
    Replies:
    1
    Views:
    199
    Vikram Karandikar
    Oct 25, 2013
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    132
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page