Inherited Templates - C++ [duplicate] - c++

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

Related

Class template value without template arguments error [duplicate]

This question already has answers here:
What are some uses of template template parameters?
(10 answers)
Closed 2 years ago.
I have some code:
template<typename Container = std::vector> // there is error
class SomeClass {
struct SomeDataStructure {
int a;
float b;
};
Container<SomeDataStructure> container; // and there
};
I want to pass template class type in template arguments of other template class.
In this example, I want to pass std::vector to SomeClass and define Container(std::vector) of private structure SomeDataStructure.
But I'm getting errors(MSVC compiler) C2059 and C2238.
How can I solve it?
Thanks in advance!
You need template template parameters:
template<template<class, class...> class Container = std::vector>

Derive from class template which is nested in class template, all withing a class template [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 3 years ago.
I'm trying to get this to work:
template<template<typename> class... Attributes>
struct Skills
{
template<class DERIVED>
struct Create : public Attributes<DERIVED>...
{};
};
template <class T, class SKILLS>
class Tester : public typename SKILLS::Create<Tester<T,SKILLS>>
{}
but the compiler complains with:
error C2518: keyword 'typename' illegal in base class list; ignored
This works fine if I don't derive within a class template, however.
Is there any chance to derive from the nested template class?
Why do I need that? I'm trying to implement a nicer interface for a CRTP-class template, which allows doing something like:
using MyClass = ClassWithAttributes<Skills<Plus,Minus,Equal,Clear,Size>>
You have to write
template <class T, class SKILLS>
class Tester : public SKILLS::template Create<Tester<T,SKILLS>>
{ };
I mean
(1) no typename: in the list of base classes is implicit that the arguments are types
(2) you need template, before Create
(3) use SKILLS as argument of Tester, not Skills, because you have declared the template parameter SKILLS
(4) remember the semicolon at the end of class definition

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

why I cannot use forward declaration when using template? [duplicate]

This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 8 years ago.
for non-template class, I can use forward declaration in the header file. But it gave me error when I use it in the template class.
I have to include the header file, I would like to know the reason.
class Pu;
template <typename T>
class Pt()
{
void test(Pu<T> u);
}
It works just fine, if you do it properly!
Remember, Pu is a class template, not a class, so it must be declared as such:
template <typename T> // N.B. this line
class Pu;
template <typename T>
class Pt // N.B. no "()"
{
void test(Pu<T> u);
}; // N.B. ";"
(live demo)

Why can't I call a template base class constructor with a const_iterator? [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 8 years ago.
For some reason, the following code gives the error Symbol 'TemplateBase' could not be resolved.:
template <typename T>
class TemplateBase
{
TemplateBase(std::map<std::string, T>::const_iterator anIterator)
{ }
};
class SubClass : public TemplateBase<int>
{
SubClass(std::map<std::string, int>::const_iterator anIterator) :
TemplateBase<int>(anIterator) //Error: Symbol 'TemplateBase' could not be resolved.
{ }
};
Strangely, no error appears when I remove the ::const_iterator and only std::map<std::string, int> remains:
template <typename T>
class TemplateBase
{
TemplateBase(std::map<std::string, T> aMap)
{ }
};
class SubClass : public TemplateBase<int>
{
SubClass(std::map<std::string, int> aMap) :
TemplateBase<int>(aMap) //No error.
{ }
};
Additionally, the following function gives no error either, so it really seems related to the combination of a template base class call with a const_iterator:
void function()
{
std::map<std::string, int>::const_iterator anIterator;
TemplateBase<int> aTemplateBase(anIterator); //No error
}
Is there some rule against using const_iterator as an argument for base class template constructors that I'm unaware of? Or is this a compiler bug?
I'm compiling with MinGW 64bit 4.9.0 on Windows 7, using C++11.
When you use a nested type that depends on a template type you need to use the typename keyword:
TemplateBase(typename std::map<std::string, T>::const_iterator anIterator)
{ }