CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 1999
    Location
    North Sydney, NS
    Posts
    445

    problem with multimap<T, int>::iterator class member

    I'm trying to add a multimap as a member of a template class and I'm having trouble declaring and iterator. It seems to work when I declare the multimap member but not the iterator.

    Code:
    #include <iostream>
    #include <vector>
    #include <map>
    #include <iomanip>
    using namespace std;
    
    template <class T>
    class Test{
        typedef multimap<T,int> MMap;
    
        public:
            MMap data;
            multimap<T,int>::iterator pos;
    };
    
    
    int main()
    {
    	std::cout << "Hello world!" << std::endl;
    	return 0;
    }
    The error i get is "expected ';' before pos"

    Is there a fix to this or am I asking too much?

    Thanks
    I know how to build. What to build is a completely different story.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: problem with multimap<T, int>::iterator class member

    I think this will work:
    Code:
    template <class T>
    class Test{
        typedef multimap<T,int> MMap;
    
        public:
            MMap data;
            typename  multimap<T,int>::iterator pos;
    };
    Viggy

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: problem with multimap<T, int>::iterator class member

    Hi.

    In this case, the iterator type is dependent on the type of T. Therefore, you need to use keyword typename to tell the compiler about this dependency. When you define the type directly, as in the code bellow, you don't need to use typename.

    Code:
    template <class T>
    class Test
    {
      typedef std::multimap<T,int> MMap;
    
    public:
      MMap data;
    
      //Here, you need typename.
      typename  std::multimap<T,int>::iterator dependent;
    
      //Here (no T), you don't need typename.
      std::multimap<int,int>::iterator nondepedent;
    };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured