Undefined reference to template class

What am I doing wrong here
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
finaladt.h
#ifndef FINALADT_H
#define FINALADT_H
#include <iostream>
using namespace std;

template<class Type>
class finalADT
{
public:
    finalADT();
    int choose_pizza(int pchoice);
    int choose_drink(int dchoice);
    void show_choices(char cpizza, char cdrink, int pchoice, int dchoice);
    void show_receipt(double total);
    double calculate(double total,double subTotal, int pchoice, int dchoice);
    ~finalADT();
};


template<class Type>
int finalADT<Type>::choose_pizza(int pchoice)
{
    for(;;)
    {
        cout << "What pizza would you like? [1-5]: \n";
        cin >> pchoice;
        if(1 <= pchoice && pchoice <= 5)
        {
            break;
        }
        else
        {
            cout << "Please enter 1 - 5\n";
        }
    }
    return pchoice;
}

template <class Type>
int finalADT<Type>::choose_drink(int dchoice)
{
    for(;;)
    {
        cout << "What drink would you like? [1-5]: \n";
        cin >> dchoice;
        if(1 <= dchoice && dchoice <= 5)
        {
            break;
        }
        else
        {
            cout << "Please enter 1 - 5\n";
        }
    }
    return dchoice;
}

template<class Type>
void finalADT<Type>::show_choices(char cpizza, char cdrink, int pchoice, int dchoice)
{
     cout << "Would you like a pizza? (Y/N): \n";
        cin >> cpizza;
        switch(cpizza)
        {
            case 'y':
            case 'Y':
                pchoice = choose_pizza(pchoice);
                break;
            case 'n':
            case 'N':
                break;
            default:
                cout << "No answer? OK...\n";
        }
        cout << "Would you like a drink? (Y/N): \n";
        cin >> cdrink;
        switch(cdrink)
        {
            case 'y':
            case 'Y':
                dchoice = choose_drink(dchoice);
                break;
            case 'n':
            case 'N':
                break;
            default:
                cout << "No answer? OK...\n";
        }
}
template <class Type>
double finalADT<Type>:: calculate(double total, double subTotal, int pchoice, int dchoice)
{
    switch(pchoice)
    {
        case 1:
            subTotal += 3;
            break;
        case 2:
            subTotal += 4;
            break;
        case 3:
            subTotal += 5;
            break;
        case 4:
            subTotal += 7;
            break;
        case 5:
            subTotal += 8.5;
            break;
    }
    switch(dchoice)
    {
        case 1:
            subTotal += .1;
            break;
        case 2:
            subTotal += .11;
            break;
        case 3:
            subTotal += .12;
            break;
        case 4:
            subTotal += .14;
            break;
        case 5:
            subTotal += .15;
            break;
    }
    total = subTotal * .1 + subTotal;
    return total;
}

template <class Type>
void finalADT<Type>::show_receipt(double total)
{
    cout << "Your total is: $" << total << "\n";
}
#endif // FINALADT_H 


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
main.cpp
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "finaladt.h"
using namespace std;

int main()
{
    finalADT<int> pizza;
    cout << "Here's our menu: \n";
    cout << "Pizzas: \t Drinks: \n";
    cout << " Pepperoni Pizza \t Coke \n";
    cout << " Mushroom Pizza \t Sprite \n";
    cout << " Bacon Pizza \t \t Royal \n";
    cout << " Pineapple Pizza \t 7-Up \n";
    cout << " Sausage Pizza \t \t Cali \n";
    char cpizza, cdrink, order;
    int pchoice, dchoice;
    double total = 0;
    double subTotal = 0;
    for(;;)
    {
        pizza.show_choices(cpizza, cdrink, pchoice, dchoice);
        pizza.calculate(total,subTotal,pchoice, dchoice);
        pizza.show_receipt(total);
        cout << "Would you like to order again? (Y/N): \n";
        cin >> order;
        switch(order)
        {
            case 'y':
            case 'Y':
                break;
            case 'n':
            case 'N':
                system("PAUSE");
                return 0;
            default:
                break;
        }
    }
}
Last edited on
Your errors in cpp shell (once the include file had been copied into the rest):
 In function 'int main()':
161:60: warning: 'cdrink' may be used uninitialized in this function [-Wmaybe-uninitialized]
161:60: warning: 'cpizza' may be used uninitialized in this function [-Wmaybe-uninitialized]
161:61: warning: 'pchoice' may be used uninitialized in this function [-Wmaybe-uninitialized]
161:61: warning: 'dchoice' may be used uninitialized in this function [-Wmaybe-uninitialized]

Self-explanatory, but I'm not sure you actually need arguments to this function.

/tmp/ccRgwddw.o: In function `main':
:(.text.startup+0xc): undefined reference to `finalADT<int>::finalADT()'
:(.text.startup+0x107): undefined reference to `finalADT<int>::~finalADT()'
:(.text.startup+0x11d): undefined reference to `finalADT<int>::~finalADT()'
collect2: error: ld returned 1 exit status

Also self-explanatory. You declared but didn't define the constructor or destructor.
Last edited on
In future please post the complete error messages, all of them, exactly as they appear in your development environment.

In your class definition you defined a no-argument constructor and a destuctor, but you failed to implement those functions. However since they really don't seem to be needed (at this time) you can either "default" them or delete the definition of those functions.

I also question the purpose of the class as a whole. You don't have any variables in that class so why have a class at all?

Also the template part also seems unnecessary since you are specifying all the types without the need for templates.
Ie: the following:
1
2
3
4
5
template <class Type>
void finalADT<Type>::show_receipt(double total)
{
    cout << "Your total is: $" << total << "\n";
}


could be implemented as a "free" function without any template or class required.:
1
2
3
4
void show_receipt(double total)
{
    cout << "Your total is: $" << total << "\n";
}


Last edited on
Delete the definition of those functions
I know what you're trying to say, but that's a poor choice of words IMO since it suggests
~finalADT() = delete;

OP, simply don't mention your constructors or destructors at all.
Last edited on
Topic archived. No new replies allowed.