Template Class Not Working

For this project, I need to replicate a Priority Queue and I can use an array, vector, or linked list and I chose to use a vector. In the book I'm reading, it says to create it using a specified data type, which I did and it worked, but when I changed it to a template, I'm getting a weird message when I compile. The first parameter for the add() function in main.cpp can be any data type that I throw in, but the second parameter has to be an int to get the priority.

vectorTemplate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef VECTOR_TEMPLATE_H
#define VECTOR_TEMPLATE_H

#include<vector>

template<typename T>
class VectorTemplate
{
    private:
        std::vector<T> _data;
        std::vector<int> _priority;
    public:
        void add(T, int);
        int highestPriority();
        int findIndex();
        void print();
        T remove();
};

#endif //VECTOR_TEMPLATE_H 


vectorTemplate.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
#include "vectorTemplate.h"
#include<vector>
#include<iostream>
#include<cstdlib>


template<typename T>
void VectorTemplate<T>::print()
{
    for (int i = 0; (i < _data.size() && i < _priority.size()); i++)
    {
        std::cout << _data[i] << " : " << _priority[i] << std::endl;
    }
}


template<typename T>
int VectorTemplate<T>::highestPriority()
{
    //set min value to the first item in the vector
    int min = _priority[0];
    //iterate over the vector to find the min value
    for (int i = 0; i < (_priority.size() - 1); i++)
    {
        //if the next value is less than the min,
        //next value [i+1] is set to the min
        if (min > _priority[i+1])
        {
            min = _priority[i+1];
        }
    }
    return min;
}

template<typename T>
void VectorTemplate<T>::add(T data, int priority)
{
    _data.push_back(data);
    _priority.push_back(priority);
}

template<typename T>
int VectorTemplate<T>::findIndex()
{
    int count = 0;
    //calls the highestPriority member function
    //lowest value (highest priority) is stored into a target
    int target = highestPriority();
    for (int i = 0; i < (_priority.size() - 1); i++)
    {
        //if target equals a value in the vector
        //the loop will break
        //else the count is incrememted by 1
        if (_priority[i] == target) {
            break;
        } else {
            count += 1;
        }
    }
    //count is returned
    return count;
}

template<typename T>
T VectorTemplate<T>::remove()
{
    if (_priority.size() > 0 || _data.size() > 0)
    {
        int index = findIndex();
        T temp = _data[index];

        _priority.erase(_priority.begin() + index);
        _data.erase(_data.begin() + index);

        return temp;
    }
    else {
        std::cout << "The vector is empty, cannot remove. " << std::endl;
        exit(1);
    }
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "vectorTemplate.h"
#include<vector>
#include<iostream>

int main()
{
    VectorTemplate<char> pQueue;
    pQueue.add('y', 3);
    pQueue.add('x', 4);
    pQueue.add('a', 7);
    pQueue.add('z', 1);
    pQueue.add('s', 2);

    //pQueue.print();

    return 0;
}


I am using Atom and compiling with g++.
This is the message that I'm getting in the terminal.

Undefined symbols for architecture x86_64:
  "VectorTemplate<char>::add(char, int)", referenced from:
      _main in driverEx6-5d0a2a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)



Any help would be appreciated! Thanks!
Last edited on
With template classes both the definition and implementation must be in the same compilation unit. Quite often this leads to header only implementations.


@jlb,

I just checked online, and it said that I can #include "vectorTemplate.cpp" in my main.cpp file, and that ended up working. Should I do that or have the declaration/definition in the header file? Is there a pro/con to either?
Actually that only works because only main.cpp uses the template. If you continue this practice you'll be including that source file in multiple other source files. So since many people consider including source files a bad practice, I'm going to recommend that you stick with just putting both the definition and implementation in the header file.

Later, if your template classes get really large, you can have an implementation file with some other extension, .inc for example, that you include into the bottom of your template class header file.

@jlb,

Thanks for the tip and help!
Topic archived. No new replies allowed.