从其他文件 C++ 的模板 class 调用静态方法
calling a static method from template class from other file C++
我想要一个class Sorings,在里面用模板静态方法实现一些排序方法。我读过 here 应该在 .h 文件中实现静态模板方法。这是我的代码:
Sortings.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_ */
Sortings.cpp
#include "Sortings.h"
//empty?
Practice.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;
}
但它 returns 出现以下错误:(这些行是这样的,因为我已经在 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
我不明白问题出在哪里。你能帮我理解错在哪里吗:概念上 or/and 语法上?
我是运行 Eclipse Neon.3 Release (4.6.3)
您模板化了 class 而不是静态方法!
因此,您需要使用 Sortings<int>::BubbleSort
.
而不是使用 Sortings::BubbleSort<int>
在调用中,您将模板添加到函数中,但您在 class 上声明了模板。
Sortings::BubbleSort<int>(a,6,true);
应该变成
Sortings<int>::BubbleSort(a,6,true);
template <class T>
class Sortings {
模板参数适用于class,不适用于方法。所以你必须像 Sortings<int>::BubbleSort(a,6,true);
这样称呼它。换句话说,没有名为 Sortings
的类型。相反,类型是 Sortings<int>
.
您也不需要 Sortings.cpp
因为所有内容都在头文件中。
虽然没有直接在问题中询问,但我认为您不需要 class 这里。命名空间内的简单模板化静态方法可以正常工作。像这样的东西:
namespace Sorting {
template<class T>
static void BubbleSort(T a[], int len, bool increasing) {
// ...
}
}
那你可以打电话给Sorting::BubbleSort<int>(a,6,true)
我想要一个class Sorings,在里面用模板静态方法实现一些排序方法。我读过 here 应该在 .h 文件中实现静态模板方法。这是我的代码:
Sortings.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_ */
Sortings.cpp
#include "Sortings.h"
//empty?
Practice.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;
}
但它 returns 出现以下错误:(这些行是这样的,因为我已经在 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
我不明白问题出在哪里。你能帮我理解错在哪里吗:概念上 or/and 语法上?
我是运行 Eclipse Neon.3 Release (4.6.3)
您模板化了 class 而不是静态方法!
因此,您需要使用 Sortings<int>::BubbleSort
.
Sortings::BubbleSort<int>
在调用中,您将模板添加到函数中,但您在 class 上声明了模板。
Sortings::BubbleSort<int>(a,6,true);
应该变成
Sortings<int>::BubbleSort(a,6,true);
template <class T>
class Sortings {
模板参数适用于class,不适用于方法。所以你必须像 Sortings<int>::BubbleSort(a,6,true);
这样称呼它。换句话说,没有名为 Sortings
的类型。相反,类型是 Sortings<int>
.
您也不需要 Sortings.cpp
因为所有内容都在头文件中。
虽然没有直接在问题中询问,但我认为您不需要 class 这里。命名空间内的简单模板化静态方法可以正常工作。像这样的东西:
namespace Sorting {
template<class T>
static void BubbleSort(T a[], int len, bool increasing) {
// ...
}
}
那你可以打电话给Sorting::BubbleSort<int>(a,6,true)