C++ Class Templates Inheritance Call Constructor/Destructor

379 views Asked by At

I have the following class.

template <class T>
class DivideConquerTask {
public:
DivideConquerTask() = delete;
DivideConquerTask(int problemSize, int branchingSize);
virtual ~DivideConquerTask();
DivideConquerTask(const DivideConquerTask& other) = delete;
DivideConquerTask& operator=(const DivideConquerTask& rhs) = delete;
DivideConquerTask(DivideConquerTask&& other) = delete;
DivideConquerTask& operator=(DivideConquerTask&& rhs) = delete;
...

and in cpp file

template <class T>
DivideConquerTask<T>::DivideConquerTask(int problemSize, int branchingSize) :
    _problemSize { problemSize }, _branchingSize { branchingSize } {
}

template <class T>
DivideConquerTask<T>::~DivideConquerTask() {
    /* Currently no resources managed. */
}

I then have a class:

class MergeSortTask final : public DivideConquerTask<int> {
public:
virtual ~MergeSortTask();
MergeSortTask(int data[], int problemSize, int branchingSize);
...

with cpp file

MergeSortTask::~MergeSortTask() {
}
MergeSortTask::MergeSortTask(int* data, int problemSize, int branchingSize) :
    DivideConquerTask(problemSize, branchingSize), _data { data } {
}

and from this I get error messages:

undefined reference to Actors::DivideConquerTask<int>::~DivideConquerTask() undefined reference toActors::DivideConquerTask::DivideConquerTask(int, int)

where Actors is the namespace everything is declared/defined in.

What's the problem here? Why can't it find my base constructor/destructor?

Note: I have omitted some trivial functions that are working fine.

1

There are 1 answers

0
Sebastian Mach On BEST ANSWER

At the point of instantiation, the template definition must be visible.

The canonical, general solution is to put your definitions in header files.