Questions tagged [template-classes]
184 questions
15
votes
5 answers
How to have a const variable in a for loop for the generation of template classes?
I have a code like
template
class A
{
template
someFunctions() {};
};
Now I want to create instances of the class and call the functions in it in a for loop for a set of many values like
// in main()
int main()
{
…

NSK
- 180
- 2
- 12
11
votes
2 answers
Ambiguous multiple inheritance of template classes
I've got a real situation which can be summarized in the following example:
template< typename ListenerType >
struct Notifier
{
void add_listener( ListenerType& ){}
};
struct TimeListener{ };
struct SpaceListener{ };
struct A : public…

nyarlathotep108
- 5,275
- 2
- 26
- 64
10
votes
1 answer
Different code path according to different type in a template method
Let's say I have a template class like this:
template
class handler
{
private:
TResponse process_core(const TRequest& request);
public:
TResponse process(const TRequest& request)
{
…

Cheng Chen
- 42,509
- 16
- 113
- 174
9
votes
3 answers
C++ class template undefined reference to function
I keep getting undefined reference when i call the two functions from my template class "add" and "greater" in my main function.
So, i have:
number.h
#ifndef NUMBER_H
#define NUMBER_H
template
class number {
public:
T x;
T y;
…

MihaiGrad
- 105
- 1
- 1
- 5
8
votes
2 answers
c++11 inheriting template constructors
I find the syntax of the constructor inheritance slightly odd. The example below works well, but I do not understand why I need to specify using sysTrajectory::sysTrajectory and not using sysTrajectory::sysTrajectory when inheriting from…
user1391279
8
votes
2 answers
Constants defined in template classes
Possible Duplicate:
GCC problem : using a member of a base class that depends on a template argument
I thought I was familiar with C++, but apparently not familiar enough.
The problem is when you define a constant in a template class, you can use…

Mr Lister
- 45,515
- 15
- 108
- 150
6
votes
2 answers
Template class type-specific functions
Ok, so i have this template class, which is kinda like one-way list.
template List
and it have this inside function print
public:
void Print();
which, as you can guess, prints the list contents from begining to end;
However, as…

Yury Elburikh
- 75
- 1
- 1
- 8
5
votes
1 answer
How to Dynamically Cast a Template Class
I'm trying to write an API around the following class template:
template
class MyClass {
public:
A foo;
B bar;
MyClass();
MyClass(A in1, B in2) {
foo = in1; bar = in2;
}
…
user5739133
5
votes
2 answers
Design a base iterator for a base class
I'm currently designing an interface (Base in the following example) which offers a couple of virtual methods including begin() and end(). These two methods simple return the corresponding iterator like in any other collection like class. Derived…

tea2code
- 1,007
- 1
- 8
- 15
5
votes
2 answers
SFINAE in class template constructors
I'm trying to make somthing with templates and SFINAE, in which I'm a beginner. I'm wasting a huge amount of time to make work every simplest thing. Can you help me understand how it works ?
The constructor of C< T , Ts... > takes a T parameter…

Gilles
- 129
- 7
4
votes
2 answers
How to pass a Callable object in c++17 to be used with std::invoke
Why the following does not compile when passing i to the constructor. The other similar constructs compile.
#include
#include
int RetXPrintU(int x, uint u)
{
std::cout << "RetXprintU(): " << u << std::endl;
return…

pettitpeon
- 87
- 4
4
votes
1 answer
Specialize a template class?
I am attempting to write a program that outputs 1 to 1000 without a loop or recursive function call, and I come up with this
#include
template
class NumberGenerator : public NumberGenerator{
public:
…

iBug
- 35,554
- 7
- 89
- 134
4
votes
1 answer
Why would a template class have an unused type?
I'm reviewing the boost units library and I am puzzled why the boost::units::unit class has an extra template parameter. Here is the example:
http://www.boost.org/doc/libs/1_57_0/boost/units/unit.hpp
template

Raymond Valdes
- 1,161
- 1
- 9
- 15
3
votes
2 answers
define non-template function outside a templated class
I was learning about non-template friend function & template friend function to a templated class. So I tried the code below :
#include
template
class cl
{
private :
T val;
public:
cl()= default;
explicit…

Blood-HaZaRd
- 2,049
- 2
- 20
- 43
3
votes
2 answers
Compiler error `assigning incompatible type within if statement`
The compiler keeps assigning incompatible types during the build.
error Message:
error: assigning to 'int' from incompatible type 'QString'
typeduserproperty.cpp:115:28: note: in instantiation of member function…

shano12
- 61
- 2