【发布时间】:2017-05-17 10:18:41
【问题描述】:
我想要一个类Sorings,在其中通过带有模板的静态方法来实现一些排序方法。我读过here 说静态模板方法应该在.h 文件中实现。这是我的代码:
排序.h
#ifndef SORTINGS_H_
#define SORTINGS_H_
template <class T>
class Sortings {
public:
static void BubbleSort(T a[], int len, bool increasing)
{
T temp;
for(int i = len-1; i >= 1; i--)
for(int j = 0; j<= i-1; j++)
if(increasing ? (a[j] > a[j+1]) : (a[j] < a[j+1])) {
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
};
};
#endif /* SORTINGS_H_ */
排序.cpp
#include "Sortings.h"
//empty?
练习.cpp
#include <iostream>
#include "Sortings.h"
int main() {
int a[6] = {4,5,2,11,10,16};
Sortings::BubbleSort<int>(a,6,true);
return 0;
}
但它返回以下错误:(这些行是这样的,因为我已经在 Practice.cpp 中注释了所有其余部分)
Description Resource Path Location Type
'template<class T> class Sortings' used without template parameters PracticeCpp.cpp /PracticeCpp/src line 41 C/C++ Problem
expected primary-expression before 'int' PracticeCpp.cpp /PracticeCpp/src line 41 C/C++ Problem
Function 'BubbleSort' could not be resolved PracticeCpp.cpp /PracticeCpp/src line 41 Semantic Error
Symbol 'BubbleSort' could not be resolved PracticeCpp.cpp /PracticeCpp/src line 41 Semantic Error
我不明白问题出在哪里。你能帮我理解什么是错误的吗:概念上或/和句法上?
我正在运行 Eclipse Neon.3 Release (4.6.3)
【问题讨论】:
标签: c++ templates static-methods