Questions tagged [template-classes]

184 questions
1
vote
4 answers

Is it possible to use inside a template class, a vector of class stack elements?

I have a "stack.h" and a "stack.cpp" file that define a hand-made stack class. What i want to do now, is to create another class "name" that has in it's composition a vector of "nr" stacks and i'm not sure where to start. depou.h: #pragma…
MihaiGrad
  • 105
  • 1
  • 1
  • 5
1
vote
2 answers

What is wrong with this simple C++ template class?

The following snippet is in a header file: // Represents NxN scalar values (aka square matrix). template class dummy { public: float& operator[](const std::size_t ind) { return scalars[ind]; } private: float…
pepe
  • 814
  • 8
  • 12
1
vote
3 answers

c++ : template member addition function

I have an interface IOperand : class IOperand { public: virtual IOperand * operator+(const IOperand &rhs) const = 0; virtual std::string const & toString() const = 0; } And the class Operand : template class Operand : public…
Jérémy Pouyet
  • 1,989
  • 6
  • 28
  • 55
1
vote
1 answer

How to overload operator= in a templated class with a dynamically sized array

I'm new to making my own template classes in C++, and after several hours of searching online for answers and toying with the function & its parameters, I've given up. I'm having run-time trouble with the following class' "=" operator: In…
A Frayed Knot
  • 476
  • 5
  • 20
1
vote
1 answer

Using an iterator template class as a parameter to a function

return Graph_iterator_Vertex(Vertexs.begin()); I do not understand why this line is called the copy constructor, although there is my constructor with a parameter. The arguments constructor parameter written specifically for this design. All that…
ggoha
  • 1,996
  • 3
  • 23
  • 31
1
vote
1 answer

Usage of template class with template method in C++

I have a class, which have a public templated methods. This class has 2 strategies of behavior, which i want to pass via class template. template class SomeClass { public: template void ProcessType(){} }; // And do…
dr11
  • 5,166
  • 11
  • 35
  • 77
1
vote
3 answers

Defining a function template as a callback for a class template

I want to define a class template that takes a callback function of the same type. Something like: typedef template bool CallbackFn( T x ); template class MyClass { public: MyClass() {} ~MyClass()…
jensph
  • 763
  • 1
  • 10
  • 22
1
vote
1 answer

Dynamic Allocation in Template Class Constructor

I am working on a stack class and have two constructors. One of interest is this one. template stack::stack( const int n) { capacity = n ; size = 0 ; arr = new T [capacity] ; } I am calling it inside main like this. stack
Sasha
  • 492
  • 2
  • 6
  • 21
0
votes
2 answers

How to create a initializer list constructor using a custom template subclass?

Basically, what I wanna do is be able to construct an object using a list of primitives like this: int main() { // C primitives initialization list int matrix[2][2] = { { 1, 2 }, { 2, 3 }, }; // C++ stl class initialization list std::string…
0
votes
0 answers

Add element to own c++ template class with overloading operator+=

I have to write a header file to an existing cpp with making an own template class. There is a simple add method but I have to overload the operator+= function. If I try to add the elements separately, like this: ls += lmb; ls += lmc; than it works…
0
votes
1 answer

'std::logic_error' what(): basic_string::_M_construct null not valid

I have this cpp file and I have to write write a header for it, with an own template class. When I try to run the code it throws this 'std::logic_error' what(): basic_string::_M_construct null not valid error. It causes by the…
0
votes
1 answer

Im making a list with iterators and node classes nested in it but for some reason the iterator class is not making the list class a friend

template class List { class Node { public: Node() { next = 0; data = 0; } Node* next; T data; }; Node* head = new Node; Node* tail = new Node; int size = 0; public: class Iterator { Node* curr; …
ppp
  • 1
  • 1
0
votes
1 answer

What exactly does most specialized class mean in C++?

Let's say we have the following: template class A {} template class A {} template class A {} Now, I know that we need to select the most specialized class, but for…
0
votes
1 answer

Overloading operator in class template with more than one template parameter

Disclaimer: this is a homework assignment, but nothing in the notes or our textbook seems to answer my question, so I'm coming here. I have a Queue template class, and one of the things I need to do is overload the + operator to merge two Queue…
0
votes
0 answers

Mock is returning null after constructors is called

I want to mock the hidden property Identity, but MyChannelMock.Object.Identity is null after constructor is called. The class looks like this: public abstract class CommunicationRouter where TChannelType : class,…