Template class as a member variable

As the title suggest I am trying to use a separate template class inside of a new template class. I am confused on the syntax. Also I am going to be using the functions inside of my List_3358 class and Im not sure how to go about implementing that. Any help would be greatly appreciated! Also im not sure how to format into code here on the forum.

// File name: stack_3358.h
//
// a Stack ADT
//
// This implementation uses a List_3358
// This implementation is a template


#include <assert.h>

using namespace std;

template <class ItemType>
class Stack_3358 {

public:

//***********************************************************
// Stack_3358: default constructor
// Preconditions: None.
// Postconditions: stack is empty.

Stack_3358();

//***********************************************************
// Stack_3358: copy constructor
// Preconditions: None.
// Postconditions: New stack is created, which is a deep copy of src stack.

Stack_3358(const Stack_3358 & src);

//****************************
// makeEmpty: Removes all the items from the stack.
// Preconditions: None.
// Postconditions: All the items have been removed

void makeEmpty();

//****************************
// isEmpty: Checks to see if there are any items on the stack.
// Preconditions: None.
// Postconditions: Returns true if there are no items on the stack,
// else false.

bool isEmpty() const;

//****************************
// isFull: Determines if you have any more room to add items to the stack.
// Preconditions: None.
// Postconditions: Returns true if there is no more room to add items,
// else false.

bool isFull() const;

//****************************
// push: Adds newItem to the top of the stack.
// Preconditions: Stack is not full.
// Postconditions: newItem is at the top of the stack.

void push(const ItemType &);

//****************************
// pop: Removes topItem from stack and returns it.
// Preconditions: Stack is not empty.
// Postconditions: Top element has been removed from stack and a copy of the
// removed element is returned.

ItemType pop();


private:
//Template class should go here.

};

******Below is the template class I'm trying to declare inside my other class.

#include <assert.h>
#include <iostream>

using namespace std;

template <class ItemType>
class List_3358 {
Last edited on
1
2
3
4
5
6
7
8
9
10
template <typename T> class H {
public:
    template <typename I> class G{};
};

int main() {
    H<int> h;
    H<int>::G<bool> g;
    return 0;
}
Last edited on
Just do it like you would do usually with nested classes and put the template keyword before it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <typename T1>
class Stack_3358
{
public:

private:
    template <typename T2> 
    class List_3358 
    {
    public:
        T2 data;
    };
 
    List_3358<T1> item;
};


Note: you have to explicitly tell the compiler what type it should be anyway.
Since your Stack class only takes 1 Template argument, the case might be, that the nested class doesn't need to be a template class at all and just has the Same type as given in the Stack.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T1>
class Stack_3358
{
public:

private:
    class List_3358 
    {
    public:
        T1 data;
    };
 
    List_3358 item;
};
Last edited on
Topic archived. No new replies allowed.