0

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.

Community
  • 1
  • 1
JTK
  • 1,469
  • 2
  • 22
  • 39
  • Does your compiler support c++11? – Jonathan Potter Mar 21 '15 at 20:56
  • @ Jonathan Potter, Visual studio 2013, pretty sure my compiler supports c++11 – JTK Mar 21 '15 at 21:04
  • 1
    @Johntk Technically, it's a class template rather than a template class. What exactly do you mean by "typedef for templates seems to be illegal"? You should be able to declare `typedef TLLNode* TLLPtr` inside the `TLL` class template. There's no need to declare that in the `TLLNode` class given this piece of code, but if you do that and want to use it outside of `TLLNode`, the syntax becomes a little more messy: `typename TLLNode::TLLPtr`. – JorenHeit Mar 21 '15 at 21:25
  • @JorenHeit I've corrected the Title, I used the word seems because I wasn't 100% sure that using typedef the way I normally would was possible, are you saying it is? I'll move the typedef to TLL and see what mileage I get. – JTK Mar 21 '15 at 21:32
  • 1
    @Johntk You definately could, but then you'd need to use the `typename` keyword to convince the compiler that the field inside `TLLNode` you're referencing is a type rather than something else. So if you have a choice, maybe just declaring it inside `TLL` makes more sense. The C++11 solution is definitely more clean though, but it never hurts to know how to solve these problems outside of C++11. – JorenHeit Mar 21 '15 at 21:34

1 Answers1

1

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

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
  • I'd spotted the missing from my TLLPtr, I've changed my code accordingly, but I still have issues with my code, I'm going to figure them out before I mark this as correct. – JTK Mar 21 '15 at 21:30
  • I've changed it to `T data;`, I'm getting an error: tll.h(21) class template has already been declared as a non-class template", not sure what to make of it. – JTK Mar 21 '15 at 21:56
  • It was my Friend deceleration, i forgot to declare it as a template. – JTK Mar 21 '15 at 22:06