0

I'm having a bad time trying to call a correctly-working expression (ultra[currentSensor].ping_timer(echoCheck)) from an own class method (myclass::run) instead of from void loop().

I'm getting the following compiler error: "invalid use of non-static member function".

If I call ultra[currentSensor].ping_timer(echoCheck) directly from void loop() it gives me no error, so I may be missing something.

Here's the (shortened) piece of code which is generating the error:

#include <NewPing.h>

#define ULTRA_NUM      3
#define MAX_DISTANCE 200

class myclass {
  public:

    void run();
    void echoCheck();

    unsigned int distance[ULTRA_NUM];
    uint8_t currentSensor = 0;
};

NewPing ultra[ULTRA_NUM] = {
  NewPing(4, 5, MAX_DISTANCE),
  NewPing(6, 7, MAX_DISTANCE),
  NewPing(8, 9, MAX_DISTANCE)
};

void myclass::run() {
// ....
      ultra[currentSensor].ping_timer(echoCheck);  // Compiler error
  }

void myclass::echoCheck() {
  if (ultra[currentSensor].check_timer())
    distance[currentSensor] = ultra[currentSensor].ping_result
}

ping_timer() is defined as it follows:

void NewPing::ping_timer(void (*userFunc)(void), unsigned int max_cm_distance) {
    if (max_cm_distance > 0) set_max_distance(max_cm_distance);

    if (!ping_trigger()) return;
    timer_us(ECHO_TIMER_FREQ, userFunc);
}

I hope any of you can help me!

Thanks in advance,

Eduardo


Edit: What I am trying to do is to adapt this example (from NewPing Arduino Library) so that the sonar ping (sonar[currentSensor].ping_timer(echoCheck)) is done from a class method, and not from void loop().

1
  • This might help. There is a discussion about calling a class function from an ISR.
    – Nick Gammon
    Jan 25, 2017 at 5:20

2 Answers 2

1

The problem is that echo_check isn't void echo_check() but void echo_check(myclass *this) because it's a member function of a class.

You can make the whole thing static (along with run()) by:

class myclass {
    public:
        static void run();
        static void echo_check();
};

static void myclass::run() {
    .... 
}

void myclass::echo_check() {
    ....
}

And then you call:

myclass::run();

instead of instantiating an object.

7
  • mm seems to solve the problem, but I'm getting now compiler errors wherever the class members are used (and also inside the class definition): "invalid use of member 'Ultrabot::_xxxxxxx' in static member function" Jan 24, 2017 at 22:54
  • 1
    A static function can only call other static functions or global normal functions, or functions of other classes that are defined as static members.
    – Majenko
    Jan 24, 2017 at 22:57
  • So now I have compiling functions which can't make what they are supposed to? :/ Jan 24, 2017 at 23:08
  • I don't know since you never bothered to show us your real code, only some tiny snippet.
    – Majenko
    Jan 24, 2017 at 23:13
  • 1
    It's not about bothering to do it or not, it's about trying not to bother you with all my partially built code. I hope it's more clear now with the edit. Jan 24, 2017 at 23:25
1

Solved:

I moved the event handler out of the class, and made it static void.

Then the code compiled properly

D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.