QList inside function template [duplicate] - c++

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;
}

Related

Inherited Templates - C++ [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
When do we need a .template construct
(4 answers)
Closed 9 months ago.
I have a template base class and an inherited class. There is a function inside the baseclass which can accept difference types, I expected that I would call this from inside the inherited class with 'BaseClass::Add();' but I instead receive the error "expected primary-expression before ‘>’ token".
How do I call BaseClass::Add with U?
template <typename T>
class BaseClass
{
public:
template <typename U>
void Add() {
// Do stuff
}
};
template <typename T>
class InheritedClass : public BaseClass<T>
{
public:
template <typename U>
void Add() {
BaseClass<T>::Add<U>(); // Error here
}
};
Thanks in advance
Use this syntax
BaseClass<T>::template Add<U>()

Invalid use of incomplete type for partial specialization [duplicate]

This question already has answers here:
"invalid use of incomplete type" error with partial template specialization
(5 answers)
Template specialization of particular members?
(2 answers)
Closed 3 years ago.
Why am I getting this error in the following:
template<typename T,typename Y>
class MyContainer
{
std::vector<T> list;
public:
void append( T&& elem) ;
};
template<typename T>
void MyContainer<T,int>::append(T&& elem)
{
list.push_back(elem);
}
int main()
{
}
error: invalid use of incomplete type ‘class >MyContainer’
void MyContainer::append(T&& elem)
I initially thought that, what if someone passed a second template parameter as double, it won't work, so I need to provide a non specialized member function definition first and may be on top of that I can provide partial specialization.
But even after adding the below I still get the same error:
template<typename T, typename Y>
void MyContainer<T,Y>::append(T&& elem)
{
list.push_back(elem);
}

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

iterator in 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 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.

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();
}