我希望有一个类Sorings ,通过静态方法和模板实现一些排序方法。 我在这里读到静态模板方法应该在.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; }但它返回以下错误:(这些行是这样的,因为我已经在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发行版(4.6.3)
I want to have a class Sorings in which to realize some sorting methods by static methods with templates. I have read here that static template methods should be realized in the .h file. Here is my code:
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; }but it returns the following errors: (the lines are like that because I have commented all the rest in 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 ErrorI don't understand what the problem is. Can you help me understand what is wrong : conceptually or/and syntactically?
I am running with Eclipse Neon.3 Release (4.6.3)
最满意答案
template <class T> class Sortings {模板参数适用于类,而不适用于方法。 所以你必须把它Sortings<int>::BubbleSort(a,6,true); 。 换句话说,没有名为Sortings类型。 相反,类型是Sortings<int> 。
此外,您不需要Sortings.cpp因为一切都在头文件中。
虽然没有直接在问题中提问,但我认为你这里不需要上课。 命名空间内的简单模板化静态方法可以正常工作。 有点像这样:
namespace Sorting { template<class T> static void BubbleSort(T a[], int len, bool increasing) { // ... } }然后你可以调用Sorting::BubbleSort<int>(a,6,true)
template <class T> class Sortings {The template parameter is applicable on class, not on the method. So you have to call it like Sortings<int>::BubbleSort(a,6,true);. In other words, there is no type named Sortings. Instead, the type is Sortings<int>.
Also you don't need Sortings.cpp since everything is inside header file.
Though not asked in the question directly, I think you don't need a class here. A simple templated static method inside namespace will work fine. Something likie this:
namespace Sorting { template<class T> static void BubbleSort(T a[], int len, bool increasing) { // ... } }Then you can call Sorting::BubbleSort<int>(a,6,true)
从其他文件C ++中的模板类调用静态方法(calling a static method from template class from other file C++)我希望有一个类Sorings ,通过静态方法和模板实现一些排序方法。 我在这里读到静态模板方法应该在.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; }但它返回以下错误:(这些行是这样的,因为我已经在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发行版(4.6.3)
I want to have a class Sorings in which to realize some sorting methods by static methods with templates. I have read here that static template methods should be realized in the .h file. Here is my code:
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; }but it returns the following errors: (the lines are like that because I have commented all the rest in 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 ErrorI don't understand what the problem is. Can you help me understand what is wrong : conceptually or/and syntactically?
I am running with Eclipse Neon.3 Release (4.6.3)
最满意答案
template <class T> class Sortings {模板参数适用于类,而不适用于方法。 所以你必须把它Sortings<int>::BubbleSort(a,6,true); 。 换句话说,没有名为Sortings类型。 相反,类型是Sortings<int> 。
此外,您不需要Sortings.cpp因为一切都在头文件中。
虽然没有直接在问题中提问,但我认为你这里不需要上课。 命名空间内的简单模板化静态方法可以正常工作。 有点像这样:
namespace Sorting { template<class T> static void BubbleSort(T a[], int len, bool increasing) { // ... } }然后你可以调用Sorting::BubbleSort<int>(a,6,true)
template <class T> class Sortings {The template parameter is applicable on class, not on the method. So you have to call it like Sortings<int>::BubbleSort(a,6,true);. In other words, there is no type named Sortings. Instead, the type is Sortings<int>.
Also you don't need Sortings.cpp since everything is inside header file.
Though not asked in the question directly, I think you don't need a class here. A simple templated static method inside namespace will work fine. Something likie this:
namespace Sorting { template<class T> static void BubbleSort(T a[], int len, bool increasing) { // ... } }Then you can call Sorting::BubbleSort<int>(a,6,true)
发布评论