-
March 30th, 2007, 02:38 PM
#1
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.
-
March 30th, 2007, 02:47 PM
#2
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
-
March 30th, 2007, 05:34 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|