0

Имеются следующие файлы:

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

А теперь сам вопрос: Следующий код:

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

Вызывает следующие ошибки:

Ошибки

Подскажите, пожалуйста, что я не доглядел

1 ответ 1

1

Не помещайте определения шаблонных функций/классов в отдельные .cpp-файлы. Размещайте все только в заголовочных файлах - иначе инстанцирования не происходит, вот и имеем, что имеем.

Совет от Страуструпа (A Tour of C++, adv. 7-6):

Раздельной компиляции шаблонов нет: вы должны включать с помощью директивы #include определения шаблонов в каждую единицу трансляции, которая их использует.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.