template <typename T, typename R>
class LinkedList{
private:
class Node{
public:
R data;
Node* next;
Node(const R& el): data(el), next(NULL){}
}; // end Node class definition
int size;
Node* first;
Node* findptr(const T&);
public:
LinkedList();
~LinkedList();
void insert(const R&);
bool find(const T&, R&);
bool erase(const T&);
void display();
}; // end LinkedList class definition
template <typename T, typename R>
LinkedList<T, R>::Node* LinkedList<T, R>::findptr(const T& key){
Node* ptr = first;
while(ptr!=NULL){
if (ptr->data == key)
return ptr;
ptr = ptr->next;
}
return NULL;
}
The problem is with LinkedList<T, R>::Node*. For some reason the compiler really doesnt like the "*", but I have used code like this before just without templates and it worked just fine. Help?
Error: expected constructor, destructor, or type conversion b
efore '*' token
Error: expected `;' before '*' token
Thanks I have been looking around for a while now. After reading some of that book you found, I don't think I will ever use templates again. They just seem so confusing now.
Edit: Okay after reading that chapter, I still can't figure out how to fix my code and get rid of that error. I changed my code back to how it was originally and also I would like to note that the non-template version of this works fine for some reason:
typedef std::string KeyType; // makes KeyType a synonym for std::string
struct Article{
string title, author;
KeyType key;
};
typedefstruct Article ElementType; // makes ElementType a synonym for struct Article
class LinkedList{
private:
class Node{
public:
ElementType data;
Node* next;
Node(const ElementType &el): data(el), next(NULL){}
}; // end Node class definition
int size;
Node* first;
Node* findptr(const KeyType &key);
public:
LinkedList();
~LinkedList();
void insert(const ElementType&);
bool find(const KeyType&, ElementType&);
bool erase(const KeyType&);
void display();
}; // end LinkedList class definition
LinkedList::Node* LinkedList::findptr(const KeyType &key){
Node* ptr = first;
while(ptr!=NULL){
if (ptr->data == key)
return ptr;
ptr = ptr->next;
}
return NULL;
}
I can't see why the template one would not work... ::bangsheadonwall::
Edit #2: I decided to just ditch the templates for this program, but it would be beneficial if anyone can tell me what is wrong for future reference. Thanks in advance.