-3

Please tell me what's wrong at code and what should I change to fix it (I get compilation error):

#include <algorithm>
#include <cstring>
using namespace std;

const int MMAX = 1000001;


//--------------------------------------------------------------------------------------------
    inline bool comp(int &A, int &B) {
        if (A < B) return true;
        return false;
    }
template<typename _CompareFunction>
    struct myHeap { // min-heap
    _CompareFunction cmp;
    };
//--------------------------------------------------------------------------------------------

myHeap< comp > H;

int main() {

}

Many thanks in advance!

Edit: Compilation error:

heap_minimal.cpp:19:15: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _CompareFunction> struct myHeap’
heap_minimal.cpp:19:15: error:   expected a type, got ‘comp’
heap_minimal.cpp:19:18: error: invalid type in declaration before ‘;’ token

(compiled with C++11)

Vlad Tarniceru
  • 711
  • 1
  • 8
  • 15

3 Answers3

1
myHeap< comp > H;

You should pass a type as template parameter, not a function. Change the declaration to the following:

myHeap<std::function<bool(int&, int&)>> H{comp};

Or

myHeap<decltype(comp)*> H{comp};

If you want to pass only template parameter(without passing function), you should declare class MyComp with overloaded operator():

struct MyComp
{
    bool operator() (int &A, int &B)
    {
        // if (A < B) return true;
        // return false;
        return A < B;
    }
};

And then just pass as parameter:

myHeap<MyComp> H;
awesoon
  • 32,469
  • 11
  • 74
  • 99
  • and then even the code won't work. Or not as he would expect. – rubenvb May 31 '13 at 14:19
  • Ok, but if you ever implemented an priority_queue, there you had **priority_queue, CompareFunction>** for example... I want the **CompareFunction** to work at me too. How do I implement this? – Vlad Tarniceru May 31 '13 at 14:21
  • `CompareFunction` here is a class with overloaded `operator()`. – awesoon May 31 '13 at 14:23
  • @Vlad `CompareFunction` can also be a non-member function, as in your example. You just need to pass the type of a function that takes two ints and returns a `bool`. – juanchopanza May 31 '13 at 14:44
1

The problem you have here is that in template definition

template<typename _CompareFunction>

_CompareFunction is a type, but then you try to use comp function in it. But you need a type, so you can fix the error like so:

myHeap< bool (*)(int&, int&) > H;

that works because bool (*)(int&, int&) is a type of your comp function. Alternatively, you can define your myHeap to take a function as template parameter

template <bool (*fun)(int&, int&)>
struct myHeap2 
{      
};

And then you can use it like this

myHeap2<comp> H2;
spiritwolfform
  • 2,263
  • 15
  • 16
0

you should define a type use "typedef bool (*comp)(int&, int&);" statement and then declare the class by pass the comp type as template parameter, like your code: myHeap< comp > H;