Typedef pointer in a Class Template

JTK

I've made a basic linked list, the list had originally held integers, I'm trying to change the list to a template class.

My node class is called TLLNode,

TLLNODE.h

#pragma once
template<class T>
class TLLNode{
    friend class TLL;
public:
    TLLNode(T data);typedef TLLNode<T>* TLLPtr;
private:
    TLLNode<T> data;
    TLLNode *next;

};

template<class T>
TLLNode<T>::TLLNode(T dataIn) : data(dataIn){

}

template<class T>
using  TLLNode<T>* TLLPtr;

and my list class to initialize and implement my functions is called TLL

TLL.h

#pragma once
#include <iostream>
#include"TLLNode.h"
using std::cout;
using std::endl;

template<class T>
class TLL{
public:
    TLL();
    TLL(const TLL&);
    void insert(T);
    void display();
    ~TLL();
private:
    TLLPtr head;
    TLLPtr newNode;
    TLLPtr curr;
    int size;
};

template<class T>
TLL<T>::TLL() : head(NULL), size(0){

}
// Not implemented yet
template<class T>
TLL<T>::TLL(const TLL& obj){

}
template<class T>
void TLL<T>::insert(T data){
    if (head == NULL)
        head = new TLLNode(data);
    else{
        newNode = new TLLNode(data);
        newNode->next = head;
        head = newNode;
    }
}
template<class T>
void TLL<T>::display(){
    curr = head;
    while (curr != NULL){
        cout << curr->data << endl;
        curr = curr->next;
    }
}
template<class T>
TLL<T>::~TLL(){
    while (head != NULL){
        curr = head;
        head = head->next;
        delete curr;
    }
}

I had been using typedef TLLNode* TLLPtr; when the list was of type int, but typedef for templates seems to be illegal.

I've tried a few different ways to get this to work and can't get any solution to work, I came across this post using, I've tried to use this solution without success, the code seems identical to what I want to do, bar the use of pointers.

I haven't tried the older solution in that post yet, using a struct. Are either solutions going to work for me and if not is there an alternative?

I realize there's probably mistakes in my code other than the issue I'm discussing, I'd like to try to figure out my own mistakes, so if it's not related to my issue or directly preventing me from proceeding, I'll figure it out when I get to it.

Anton Savin

You should define TLLPtr as follows:

template<class T>
using TLLPtr = TLLNode<T>*;

And everywhere in the definition of TLL use TLLPtr<T> instead of just TLLPtr (and also change TLLNode to TLLNode<T>).

Update: I've also noticed that you have TLLNode<T> data; in TLLNode. You can't define a member of the class having the type of class itself, you probably meant T data;.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to typedef a smart pointer wrapping a template class

From Dev

Typedef Template Class Pointer in*.cpp file

From Dev

typedef template class instances

From Dev

typedef for template class

From Dev

Using typedef with a template class

From Dev

typedef template class instances

From Dev

How do i use typedef in c++ on a function pointer that must return an class template?

From Dev

SFINAE failure with typedef in class template referring to typedef in another class template

From Dev

Forward declaring a typedef for a template class

From Dev

Template typedef is not a class or namespace name

From Dev

How to typedef a function pointer with template arguments

From Dev

How to refer to typedef defined in template class

From Dev

Function pointer to class template member

From Dev

Template detects if T is pointer or class

From Dev

Template class with function pointer parameter

From Dev

Member function pointer of template class

From Dev

error C2823: a typedef template is illegal - function pointer

From Dev

Define a function pointer with typedef in a class to a parent class function

From Dev

Specialization of template class with pointer template argument

From Dev

Template function pointer of template class - C++

From Dev

Aliasing the member template typedef with using makes compiler think it is not a class template

From Dev

Can I use a class level typedef as template argument for the base class?

From Dev

Base class pointer for a template class that uses template parameter in member data

From Dev

Is it allowed to typedef a class template type argument into the same name?

From Dev

How to typedef a template class with typdef name C++

From Dev

Template function in base class using this-pointer

From Dev

Passing a pointer-to-class-member as a template parameter

From Dev

how to pass pointer to member function of a template class?

From Dev

Scope Resolution to a Static Function or a Pointer in Template Class

Related Related

  1. 1

    How to typedef a smart pointer wrapping a template class

  2. 2

    Typedef Template Class Pointer in*.cpp file

  3. 3

    typedef template class instances

  4. 4

    typedef for template class

  5. 5

    Using typedef with a template class

  6. 6

    typedef template class instances

  7. 7

    How do i use typedef in c++ on a function pointer that must return an class template?

  8. 8

    SFINAE failure with typedef in class template referring to typedef in another class template

  9. 9

    Forward declaring a typedef for a template class

  10. 10

    Template typedef is not a class or namespace name

  11. 11

    How to typedef a function pointer with template arguments

  12. 12

    How to refer to typedef defined in template class

  13. 13

    Function pointer to class template member

  14. 14

    Template detects if T is pointer or class

  15. 15

    Template class with function pointer parameter

  16. 16

    Member function pointer of template class

  17. 17

    error C2823: a typedef template is illegal - function pointer

  18. 18

    Define a function pointer with typedef in a class to a parent class function

  19. 19

    Specialization of template class with pointer template argument

  20. 20

    Template function pointer of template class - C++

  21. 21

    Aliasing the member template typedef with using makes compiler think it is not a class template

  22. 22

    Can I use a class level typedef as template argument for the base class?

  23. 23

    Base class pointer for a template class that uses template parameter in member data

  24. 24

    Is it allowed to typedef a class template type argument into the same name?

  25. 25

    How to typedef a template class with typdef name C++

  26. 26

    Template function in base class using this-pointer

  27. 27

    Passing a pointer-to-class-member as a template parameter

  28. 28

    how to pass pointer to member function of a template class?

  29. 29

    Scope Resolution to a Static Function or a Pointer in Template Class

HotTag

Archive