Templating Errors

So I've been working on this program to maintain LinkedList, and I've gone to now use templates, for code reusability and such.

I'm getting the error undefined reference to LinkedList<float>::clear()
as well as all other functions.

im not sure how to approach this error, i would write a function for the float case but that defeats the purpose of templating. Anyways thanks for any help

the code

main .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #include <iostream>
#include "LinkedList.h"

using namespace std;

bool badOp(int);\
template<class type>
void selectCase(int, LinkedList<type>*);
template<class type>
void createList(LinkedList<type>*);
template<class type>
void addTo(LinkedList<type>*);
template<class type>
void removeFrom(LinkedList<type>*);
template<class type>
void clearList(LinkedList<type>*);
template<class type>
void changeName(LinkedList<type>*);

int main()
{
    LinkedList<float> * theList=new LinkedList<float>();
    int op=0;
    cout<<"Welcome user, how can we help you?"<<endl;
    while(op!=7){
        cout<<"\n(1) Create a blank list.\n(2)Add to existing list\n(3)Remove from existing list.\n(4)Clear existing list\n(5)Change name associated with list\n(6) Print list\n (7) Exit Program\n\n";
        cout<<"Please enter a number from 1-7\n";
        cin>>op;
        while(badOp(op)){
            cout<<"\nInvalid Entry, please reenter your selection.\n";
            cin>>op;
        }
        selectCase(op, theList);
    }
}

template<class type>
void selectCase(int op, LinkedList<type>* theList){
    switch(op){
        case 1:
            createList(theList);
            break;
        case 2:
            addTo(theList);
            break;
        case 3:
            removeFrom(theList);
            break;
        case 4:
            clearList(theList);
            break;
        case 5:
            changeName(theList);
            break;
        case 6:
            theList->print();
            break;
        case 7:
            cout<<theList;
            cin>>op;
            break;
    }
}

bool badOp(int op){
    return (op>7 || op<1);
}

template<class type>
void createList(LinkedList<type>* l){
    l->clear();
    cout<<"\nWhat will you name the new list?\n";
    std::string n;
    cin>>n;
    l->setName(n);
}

template<class type>
void addTo(LinkedList<type>* l){
    int n;
    cout<<"\nWhat grade would you like to add to the list?\n";
    cin>>n;
    if(n>=0)
        l->add(n);
    else
        cout<<"\nThe grade must be a positive number.\n";
}

template<class type>
void removeFrom(LinkedList<type>* l){
    if(!l->isEmpty()){
        l->remove();
        cout<<"\nThe last added file has been removed.\n";
    }
    else{
        cout<<"This list is empty.";
    }
}

template<class type>
void clearList(LinkedList<type>* l){
    l->clear();
    cout<<"\nThe list has been cleared.\n";
}

template<class type>
void changeName(LinkedList<type>* l){
    std::string name;
    cout<<"\nWhat would you like to rename your list?\n";
    cin>>name;
    l->setName(name);
}


header for linked list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<string>
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template<class type>
struct Node{
    type data;
    Node * next;
};
template<class type>
class LinkedList
{
    public:
        LinkedList();
        LinkedList(std::string);
        virtual ~LinkedList();
        void add(type);
        type remove();
        void clear();
        type peek();
        bool isEmpty();
        int getSize();
        std::string getName();
        void setName(std::string);
        void print();

    private:
        std::string studentName;
        Node<type> * headPointer;
        int size;
};

#endif 


cpp file for linkedlist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "LinkedList.h"
#include<iostream>

template<class type>
LinkedList<type>::LinkedList()
{
    studentName="";
    headPointer=NULL;
    size=0;
}

template<class type>
LinkedList<type>::LinkedList(std::string name)
{
    studentName=name;
    headPointer=NULL;
    size=0;
}

template<class type>
LinkedList<type>::~LinkedList()
{
    clear();
}

template<class type>
void LinkedList<type>::add(type data){
    Node<type>* newNode=new Node<type>();
    newNode->data=data;
    newNode->next=headPointer;
    headPointer=newNode;
    size++;
}

template<class type>
type LinkedList<type>::remove(){
    if(!isEmpty()){
        Node<type>* temp=headPointer;
        headPointer=headPointer->next;
        type a=temp->data;
        delete temp;
        size--;
        return a;
    }
    else return -1;
}

template<class type>
void LinkedList<type>::clear(){
    print();
    while(!isEmpty()){
        remove();
        std::cout<<"p";
    }
}

template<class type>
bool LinkedList<type>::isEmpty(){
    return headPointer==NULL;
}

template<class type>
type LinkedList<type>::peek(){
    if(isEmpty()){
        return -1;
    }
    else
        return headPointer->data;
}

template<class type>
int LinkedList<type>::getSize(){
    return size;
}

template<class type>
void LinkedList<type>::setName(std::string name){
    studentName=name;
}

template<class type>
std::string LinkedList<type>::getName(){
    return studentName;
}

template<class type>
void LinkedList<type>::print(){
    Node<type>* cur=headPointer;
    std::cout<<"\nTop of list, \""<<studentName<<"\":";
    while(cur!=NULL){
        std::cout<<cur->data<<"\n";
        cur=cur->next;
    }
}
templates must all be in the header (cut/paste from the cpp).

Also, set this project aside until you get a templated function working:
1
2
3
4
5
6
7
8
9
10
11
12
//func.h
template <typename t>
void print(t data){
  std::cout << data << '\n';
}

//main.cpp

int main(void){
  print("Hello");
  print(1);
}
Topic archived. No new replies allowed.