0
class ARRAY {
  public:
    int *array;

    ARRAY(int size);

    void INSERT(int data);
};

ARRAY::ARRAY(int size) {
    array = new int[size];
}

void ARRAY::INSERT(int data) {
    array[data] = data;
    return;
}

This is a class of array.
And I want to pass a ARRAY class' method to some function's argument that measure a runtime.

typedef void (ARRAY::*arrFuncCall)(int);

clock_t measure_time(arrFuncCall func, int iterate) {
    clock_t start = clock();

    for (int i = 0; i < iterate; i++)
        func(i);

    clock_t end = clock();

    return end - start;
}

But I keep seeing an error in

for (int i = 0; i < iterate; i++)
   func(i);

That expression preceding parentheses of apparent call must have (pointer-to-) function type in func

Why am I seeing this error?

tadman
  • 208,517
  • 23
  • 234
  • 262
jadon
  • 29
  • 4
  • Is this for a C++ course? In actual C++ code you'd probably use a [lambda](https://en.cppreference.com/w/cpp/language/lambda) instead of function pointers. In object-oriented C++ code (as in most C++ code) function pointers are pretty much useless. – tadman Mar 18 '21 at 06:09
  • 3
    You need an instance of `Array` to call one of it's non-static member functions. See also [this question](https://stackoverflow.com/questions/400257/how-can-i-pass-a-class-member-function-as-a-callback), maybe even a dupe. – Lukas-T Mar 18 '21 at 06:10
  • actually its DS course.,, maybe I should try find another way to implement this.. – jadon Mar 18 '21 at 06:14
  • *meant `ARRAY`, whoops. – Lukas-T Mar 18 '21 at 06:17
  • What's the objective here? Using a function pointer will be trouble unless it's literally just a plain (non-class) function, or at least a `static` one. If you just need to time a call, use a lambda. It's reliable, it works, and unless they're teaching you C++98 or C++79 or something equally ridiculous then you can surely use them. – tadman Mar 18 '21 at 06:21
  • *"measure a runtime"* -- a runtime of what? The time of inserting an element into an `ARRAY` using `ARRAY::INSERT()`? Well, you can specify "inserting" with your function pointer and "element" with `i`, but which `ARRAY`? – JaMiT Mar 18 '21 at 07:14

0 Answers0