The following files are available:

SortedSequence.h:

#ifndef SORTEDSEQUENCE_H #define SORTEDSEQUENCE_H #include "seqiterator.h" template<class T> class SortedSequence { protected: int length; public: SortedSequence(); virtual ~SortedSequence(); bool empty(); virtual SeqIterator<T> begin() = 0; virtual SeqIterator<T> end() = 0; virtual void add(const T & element, int (*comparator)(const T&, const T&)) = 0; virtual void add(const T & element) = 0; }; #endif // SORTEDSEQUENCE_H 

SortedSequence.h:

 #include "SortedSequence.h" template<class T> SortedSequence<T>::SortedSequence() : length(0) {} template<class T> SortedSequence<T>::~SortedSequence() {} template<class T> bool SortedSequence<T>::empty() { return this->length == 0; } 

ASortedSequence.h:

 #ifndef ASORTEDSEQUENCE_H #define ASORTEDSEQUENCE_H #include "SortedSequence.h" template<class T> class ASortedSequence : public SortedSequence<T> { T * array; public: explicit ASortedSequence(); ~ASortedSequence(); SeqIterator<T> begin(); SeqIterator<T> end(); void add(const T & element, int (*comparator)(const T&, const T&)); void add(const T & element); }; #endif // ASORTEDSEQUENCE_H 

ASortedSequence.cpp:

 #include "ASortedSequence.h" template<class T> ASortedSequence<T>::ASortedSequence() { this->array = nullptr; this->length = 0; } template<class T> ASortedSequence<T>::~ASortedSequence() { if (this->length == 0) return; else delete[] array; } template<class T> void ASortedSequence<T>::add(const T & element, int (*comparator)(const T &, const T &)) { // ... } template<class T> void ASortedSequence<T>::add(const T & element) { // ... } template<class T> SeqIterator<T> ASortedSequence<T>::begin() { return SeqIterator<T>(array); } template<class T> SeqIterator<T> ASortedSequence<T>::end() { return SeqIterator<T>(0); } 

seqiterator.h:

 #ifndef SEQITERATOR_H #define SEQITERATOR_H #include <iterator> template<class T> class SeqIterator : public std::iterator<std::forward_iterator_tag, T> { T * currentPlace; public: SeqIterator(T * ptr); SeqIterator(const SeqIterator<T> & iter); SeqIterator<T> & operator++ (); SeqIterator<T> operator++ (int); bool operator== (const SeqIterator<T> & r); bool operator!= (const SeqIterator<T> & r); T & operator* (); }; #endif // SEQITERATOR_H 

seqiterator.cpp:

 #include "seqiterator.h" template<class T> SeqIterator<T>::SeqIterator(T * ptr) : currentPlace(ptr) {} template<class T> SeqIterator<T>::SeqIterator(const SeqIterator<T> & iter) : currentPlace(iter.currentPlace) {} template<class T> SeqIterator<T> & SeqIterator<T>::operator++ () { ++currentPlace; return *this; } template<class T> SeqIterator<T> SeqIterator<T>::operator++ (int) { SeqIterator<T> tmp (*this); operator ++(); return tmp; } template<class T> bool SeqIterator<T>::operator== (const SeqIterator<T> & r) { return currentPlace == r.currentPlace; } template<class T> bool SeqIterator<T>::operator!= (const SeqIterator<T> & r) { return currentPlace != r.currentPlace; } template<class T> T & SeqIterator<T>::operator* () { return *currentPlace; } 

.pro:

 QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = Search TEMPLATE = app SOURCES += main.cpp\ widget.cpp \ SortedSequence.cpp \ ASortedSequence.cpp \ seqiterator.cpp HEADERS += widget.h \ SortedSequence.h \ ASortedSequence.h \ seqiterator.h FORMS += widget.ui 

And now the question itself: The following code:

 ASortedSequence<int> seq; // … for (…) seq.add(number.toInt()); // … for (auto iter = seq.begin(); iter != seq.end(); ++iter) ui->output->append(QString(*iter)); 

Causes the following errors:

Errors

Tell me, please, what I did not see

    1 answer 1

    Do not put definitions of template functions / classes in separate .cpp files. Place everything only in the header files - otherwise instantiation does not occur, so we have what we have.

    Advice from Stroustrup (A Tour of C ++, adv. 7-6):

    There are no separate compilation of templates: you must include the definition of templates in each translation unit that uses them using the #include directive.