iterator in template class [duplicate] - c++

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 9 years ago.
I have the following template class.
template <typename _Type, typename _Comparator = equal_to<_Type> >
class CSearch
{
...
};
It should store STL stuffs like list, set or string.
I store all elements (for example strings) in private class member:
map<int,_Type> seqs;
Now I want to use iterator, but there is a problem with <_Type>::const_iterator.
template <typename _Type, typename _Comparator>
void CSearch<_Type,_Comparator>::Foo1(int id, const _Type & needle)
{
seqs.insert(make_pair(id,needle));
for(_Type::const_iterator it=seqs[0].begin();it!=seqs[0].end();it++)
cout<<*it<<" ";
cout<<endl;
}
or analogically
for(map<int,_Type>::const_iterator it=seqs.begin();it!=seqs.end();it++)
cout<<*it<<" ";

<_Type>::const_iterator is a dependent type.
Refer to it as typename <_Type>::const_iterator instead.

Related

How to use reinterpret cast for inner template class? [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 5 years ago.
I have two template classes as outer and inner. I am type casting to inner class from other inner class object. I am getting compilation error. How to resolve this?
template<typename O>
struct outer
{
template<typename I>
struct inner
{
};
inner<int> *ptr;
outer();
};
template<typename O,typename I>
void callme()
{
reinterpret_cast< outer<O>::inner<I> *>(NULL);
}
You want:
reinterpret_cast<typename outer<O>::template inner<I> *>(nullptr);
// ^^^^^^^^ ^^^^^^^^ ^^^^^^^
The names outer and inner are dependent names (they depend on template arguments), and therefore you need to explicitly specify their "kind" (value, type, template).

How to define a type alias for a template in a class [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 5 years ago.
For example
struct Option_1
{
template<class T> using Vector = std::vector<T>;
};
I can do
typename Option_1::Vector<int> v;
But I prefer the following
Vector<Option_1, int> v;
or similars without the word "typename". I define an alias
template<class Option, class T> using Vector= typename Option::Vector<T>;
but failed with unrecognizable template declaration/definition. How to fix it?
You should use the keyword template for the dependent template name Option::Vector, i.e.
template<class Option, class T> using Vector = typename Option::template Vector<T>;
// ~~~~~~~~
LIVE

Cannot pass template argument to std::list<T>::iterator [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 6 years ago.
I made a container template class as follow:
template<typename K, typename V>
class hash_table {
public:
class iterator {
private:
list<V> list_; // Works well
list<V>::iterator it_; // Fails: Syntax-error "iterator"
list<int>::iterator it2_; // Works well
};
//....
}
Can someone tell me, what I did wrong at list<V>::iterator it_;? Why should this be an syntax error?
As #songyuanyao sugested, the solution is to put typename before the list<V>::iterator like in:
template<typename K, typename V>
class hash_table {
public:
class iterator {
private:
list<V> list_; // Works well
typename list<V>::iterator it_; // No more fails
list<int>::iterator it2_; // Works well
};
//....
}
See also:
C++ template typename iterator

QList inside function template [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 7 years ago.
I am trying to create a function template containing a QList iterator :
template <typename T> void funcTemplate()
{
QList<T>::ConstIterator it;
}
I get:
In function 'void funcTemplate()': error: expected ';' before 'it'
I tried lots of things but I can't make it compile.
It is really strange because I can create a QList without problems:
template <typename T> void funcTemplate()
{
QList<T> list;
}
Does anybody has an idea?
Thanks!
It should be
template <typename T> void funcTemplate()
{
typename QList<T>::ConstIterator it;
}

how to return iterator from a member function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?
I have a template class like below
template <class Key, class Object>
class TObjectRegistery
{
public:
typedef map<const Key, Object*> ObjectMap;
void AddObject(Object *obj){
objectMap_[obj.code()] = obj;
}
private:
ObjectMap objectMap_;
}
I want to run an iteration outside of TFactory, then I want to add two member functions to the class.
ObjectMap::iterator xbegin(){
return objectMap_.begin();
}
but I get an error that I'm missing ; before xbegin like undefine ObjectMap::iterator
"missing ';' before identifier 'xbegin'"
why does this happen?
how I can fix it ?
if this good way to do iteration out of class?
You also need the typename keyword before ObjectMap, since it's derived type of the template parameters:
typename ObjectMap::iterator xbegin(){
return objectMap_.begin();
}