У меня есть заголовочный файл с классом шаблона:шаблон метод класса в исходном файле
#ifndef BUBBLE_H
#define BUBBLE_H
#include "algorithm.h"
template <typename T>
class Bubble : public Algorithm <T> {
public:
Bubble(T* in, int inSize) : Algorithm<T>(in, inSize){}
void compute();
};
#endif // BUBBLE_H
если я надену все тело из вычислений() класса здесь все отлично работает. Но я хотел бы иметь его в файле cpp. Я писал:
#include "bubbleSort.h" using namespace std;
template <typename T>
void BubbleSort<T>::compute(){ //(*)
for (int i = 1; i<this->dataSize; i++){
for (int j = this->dataSize-1; j>=i; j--){
if(this->data[j] < this->data[j-1]) swap(this->data[j-1], this->data[j]);
}
} }
Но я получил сообщение об ошибке в строке (*):
error: expected initializer before '<' token void BubbleSort::compute(){ ^
Как мне это исправить?
См. Также http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – juanchopanza