STL Map iterator compilation error

Discussion in 'C++' started by Rakesh Kumar, Nov 29, 2007.

  1. Rakesh Kumar

    Rakesh Kumar Guest

    I am encountering the following issue with STL map iterator - wrapped
    within a template. ( I am wrapping it withing a template since I want
    to hide the map implementation from the user . At a later point - this
    could be a custom hashmap (as opposed to rb-tree map that stl
    provides ).



    #include <string>
    #include <map>
    #include <iostream>

    template<typename K, typename V> class TestMap
    {

    public:
    void insert(const std::pair<K, V> & ap)
    {
    data.insert(ap);
    }

    void list()
    {
    std::map<K, V>::iterator it = data.begin();
    //compiler errors here - error: expected
    `;' before "it"

    for (; it != data.end(); ++it)
    {
    std::cout << it->first << " --> " << it->second << "\n";
    }
    }

    private:
    std::map<K, V> data;
    };

    using std::string;

    int main()
    {
    TestMap<std::string, int> data;

    data.insert(std::pair<string, int >("Hello", 1));
    data.insert(std::pair<string, int >("World", 1));
    return EXIT_SUCCESS;
    }


    The compiler errors out with the following message.

    test.cxx:17: error: expected `;' before "it"

    Any idea if I am missing anything here.
    Rakesh Kumar, Nov 29, 2007
    #1
    1. Advertising

  2. Rakesh Kumar

    red floyd Guest

    Rakesh Kumar wrote:
    > I am encountering the following issue with STL map iterator - wrapped
    > within a template. ( I am wrapping it withing a template since I want
    > to hide the map implementation from the user . At a later point - this
    > could be a custom hashmap (as opposed to rb-tree map that stl
    > provides ).
    >
    >
    >
    > #include <string>
    > #include <map>
    > #include <iostream>
    >
    > template<typename K, typename V> class TestMap
    > {
    >
    > public:
    > void insert(const std::pair<K, V> & ap)
    > {
    > data.insert(ap);
    > }
    >
    > void list()
    > {

    typename std::map<K,V>::iterator it = data.begin();

    std::map<K,V>::iterator is a dependent name, the compiler needs some
    help to know it's a type.

    > std::map<K, V>::iterator it = data.begin();
    > //compiler errors here - error: expected
    > `;' before "it"
    >
    > for (; it != data.end(); ++it)
    > {
    > std::cout << it->first << " --> " << it->second << "\n";
    > }
    > }
    >
    > private:
    > std::map<K, V> data;
    > };
    >
    > using std::string;
    >
    > int main()
    > {
    > TestMap<std::string, int> data;
    >
    > data.insert(std::pair<string, int >("Hello", 1));
    > data.insert(std::pair<string, int >("World", 1));
    > return EXIT_SUCCESS;
    > }
    >
    >
    > The compiler errors out with the following message.
    >
    > test.cxx:17: error: expected `;' before "it"
    >
    > Any idea if I am missing anything here.
    >
    red floyd, Nov 29, 2007
    #2
    1. Advertising

  3. Rakesh Kumar

    Rakesh Kumar Guest

    On Nov 28, 4:26 pm, red floyd <> wrote:
    >
    > typename std::map<K,V>::iterator it = data.begin();
    >
    > std::map<K,V>::iterator is a dependent name, the compiler needs some
    > help to know it's a type.



    Thanks redfloyd for the help.

    Revised code that compiles:

    #include <string>

    #include <map>
    #include <iostream>

    template<typename K, typename V> class TestMap
    {

    public:
    void insert(const std::pair<K, V> & ap)
    {
    data.insert(ap);
    }

    void list()
    {
    typename std::map<K, V>::iterator it = data.begin();
    for (; it != data.end(); ++it)
    {
    std::cout << it->first << " --> " << it->second << "\n";
    }
    }

    private:
    std::map<K, V> data;
    };

    using std::string;

    int main()
    {
    TestMap<std::string, int> data;

    data.insert(std::pair<string, int >("Hello", 1));
    data.insert(std::pair<string, int >("World", 1));
    return EXIT_SUCCESS;
    }
    Rakesh Kumar, Nov 29, 2007
    #3
    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. rg
    Replies:
    1
    Views:
    2,149
    tom_usenet
    Jul 22, 2004
  2. Steve Edwards
    Replies:
    11
    Views:
    25,552
    Mike Wahler
    Feb 23, 2006
  3. cppquest
    Replies:
    5
    Views:
    367
    Dana Good
    Feb 22, 2007
  4. Replies:
    3
    Views:
    977
    Andrew Koenig
    Apr 7, 2008
  5. Jim Anderson

    problem with iterator (map iterator)

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

Share This Page