1

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 Error

I 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)

Community
  • 1
  • 1
Veliko
  • 747
  • 1
  • 9
  • 25

3 Answers3

2
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)

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • Thank you. I realized what I did. It works and of course when I changed the template declaration from the class to the method only, my original code worked. – Veliko May 17 '17 at 10:28
1

You templated the class and not the static method!

So instead of using Sortings::BubbleSort<int> you need to use Sortings<int>::BubbleSort.

blacktemplar
  • 699
  • 1
  • 7
  • 21
1

on the call, you added the template to the function, but you declared the template on the class.

Sortings::BubbleSort<int>(a,6,true);

should become

Sortings<int>::BubbleSort(a,6,true);
Tomaz Canabrava
  • 2,320
  • 15
  • 20